简体   繁体   English

最小的承诺/ A +承诺界面

[英]Minimal interface for a Promises/A+ promise

I am writing a JavaScript code generator and want to avoid a dependency to a specific Promises/A+ framework. 我正在编写JavaScript代码生成器,并希望避免依赖于特定的Promises / A +框架。 Instead of using callbacks in methods/functions, I want to return promises. 我不想在方法/函数中使用回调,而是要返回promise。

What is the minimal required interface of a promise object so that it works with any Promises/A+ library? 一个Promise对象与任何Promises / A +库一起使用所需的最小接口是什么?

The only required interface for a Promises/A+ is implementing the then method. Promises / A +唯一需要的接口是实现then方法。

This is specified here: http://promisesaplus.com/ 在此处指定: http//promisesaplus.com/

This makes sense, since A+ is only about making promises interoperability , as a standard it only specifies the bare minimum for that. 这是有道理的,因为A +仅是要保证互操作性 ,作为标准,它仅指定了最低要求。

You can .catch with: 您可以通过以下方式.catch

promise.catch(err){

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

});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM