Promise實例的成功和失敗的回調的返回值,能夠傳遞到外層的下一個then。按照返回值類型能夠分爲如下幾種:
a.promise 成功和失敗的回調的返回值 能夠傳遞到外層的下一個then
b. 返回值處理node
c. 錯誤處理 若是離本身最近的then 沒有錯誤處理(就是沒有寫錯誤函數),會向下找
d. 每次執行完Promise.then方法後,返回的都是一個‘新的promise’(promise一旦成功或者失敗就不能修改狀態)promise
說明name.txt和age.txt文件都存在,name1.txt文件不存在函數
let fs = require("fs"); function read(filename) { return new Promise((resolve, reject) => { fs.readFile(filename, "utf8", (err, data) => { if (err) return reject(err); resolve(data); }); }); }
1.返回普通值(不是throw Error, 不是return promise 都屬於返回的普通值),傳遞到外層的下一個then的成功中。ui
read("./name1.txt").then( (data) => { return 123; }, (err) => { return err; } ).then( (data) => { console.log("---success--", data); }, (err) => { console.log("---fail--", err, "錯誤了!"); } );
輸出結果是code
---success-- [Error: ENOENT: no such file or directory, open 'd:\myproject\node-s\name1.txt'] { errno: -4058, code: 'ENOENT', syscall: 'open', path: 'd:\\myproject\\node-s\\name1.txt' }
read("./name.txt").then( (data) => { return 123; }, (err) => { return err; } ).then( (data) => { console.log("---success--", data); }, (err) => { console.log("---fail--", err, "錯誤了!"); } );
輸出結果---success-- 123
回調函數
2.throw Error/throw new Error(),傳遞到外層的下一個then的失敗中。io
read("./name1.txt").then( (data) => { return 123; }, (err) => { throw err; } ).then( (data) => { console.log("---success--", data); }, (err) => { console.log("---fail--", err, "錯誤了!"); } );
輸出結果console
---fail-- [Error: ENOENT: no such file or directory, open 'd:\myproject\node-s\name1.txt'] { errno: -4058, code: 'ENOENT', syscall: 'open', path: 'd:\\myproject\\node-s\\name1.txt' } 錯誤了!
read("./name.txt").then( (data) => { throw Error( 123); }, (err) => { throw err; } ).then( (data) => { console.log("---success--", data); }, (err) => { console.log("---fail--", err, "錯誤了!"); } );
輸出結果function
---fail-- Error: 123 at d:\myproject\node-s\2.promise\1.promise.js:32:13 錯誤了!
3.return promise, 會採用return promise的狀態,決定走外層的下一個then的成功仍是失敗require
read("./name.txt").then( (data) => { return read('./age.txt'); }, (err) => { throw err; } ).then( (data) => { console.log("---success--", data); }, (err) => { console.log("---fail--", err, "錯誤了!"); } );
輸出結果:---success-- 88
read("./name.txt").then( (data) => { return read('./age1.txt'); }, (err) => { throw err; } ).then( (data) => { console.log("---success--", data); }, (err) => { console.log("---fail--", err, "錯誤了!"); } );
輸出結果:
---fail-- [Error: ENOENT: no such file or directory, open 'd:\myproject\node-s\age1.txt'] { errno: -4058, code: 'ENOENT', syscall: 'open', path: 'd:\\myproject\\node-s\\age1.txt' } 錯誤了!
read("./name1.txt").then( (data) => { return read('./age1.txt'); }, (err) => { console.log(err); // 默認return undefined } ).then( (data) => { console.log("---success--", data); }, (err) => { console.log("---fail--", err, "錯誤了!"); } );
輸入結果:
[Error: ENOENT: no such file or directory, open 'd:\myproject\node-s\name1.txt'] { errno: -4058, code: 'ENOENT', syscall: 'open', path: 'd:\\myproject\\node-s\\name1.txt' } ---success-- undefined
read("./name1.txt").then( (data) => { return read('./age1.txt'); }).then( (data) => { console.log("---success--", data); }, (err) => { console.log("---fail--", err, "錯誤了!"); } );
輸出結果:
---fail-- [Error: ENOENT: no such file or directory, open 'd:\myproject\node-s\name1.txt'] { errno: -4058, code: 'ENOENT', syscall: 'open', path: 'd:\\myproject\\node-s\\name1.txt' } 錯誤了!