Promises/a+承诺的最小接口

Minimal interface for a Promises/A+ promise

本文关键字:接口 承诺 Promises      更新时间:2023-09-26

我正在编写一个JavaScript代码生成器,希望避免对特定Promises/a+框架的依赖。我希望返回promise,而不是在方法/函数中使用回调。

promise对象与任何Promises/a+库协同工作所需的最小接口是什么?

承诺/a+唯一需要的接口是实现then方法。

此处指定:http://promisesaplus.com/

这是有道理的,因为A+只是承诺互操作性,作为标准,它只指定了最低限度的互操作性。

您可以使用 .catch

promise.catch(err){
});
// is the same as the following, which is required by the Promises/A+ standard.
promise.then(null, function(err){
});