• JS 翻译求助

    2016/01/12 Lin 1 评论

What’s going to happen in the following snippet?
下面这段代码会发生什么?

const fs = require(‘fs’)
function readPackage (callback) {
//as of now we do not have default values in Node.js
callback = callback || function () {}
return new Promise((resolve, reject) => {
fs.readFile(‘./package.json’, (err, data) => {
if (err) {
reject(err)
return callback(err)
}
resolve(data)
return callback(null, data)
})
})
}

1.It will throw an exception in line 3
2.The catch will handle it, and print it out to the stdout: [Error: ops]
3.The execution continues and in line 9 a new error will be thrown
4.Nothing else
1.在第三行会抛出一个异常
2.catch 会处理它,并且把它打印到标准输出
3.程序继续执行,并且在第9行抛出一个新的错误
4.没有其他的了

And really nothing else – the last error thrown will be a silent one.
Pay extra attention to always add a catch as the last member of the promise chain.
求助最后这两句的翻译,希望懂promise 的来解释更好。

1 收藏


直接登录
最新评论
  • Lin   2016/01/12

    代码贴错了,sorry
    Promise.resolve(() => ‘John’)
    .then(() => {
    throw new Error(‘ops’)
    })
    .catch((ex) => {
    console.log(ex)
    })
    .then(() => {
    throw new Error(‘ups’)
    console.log(‘Doe’)
    })