Promise thenable All In One

Promise thenable 是指一个函数或一个对象的里面定义了一个 then 方法

Promises/A+

https://promisesaplus.com/#terminology

https://github.com/then/promise

https://github.com/then/thenable/blob/master/index.js


// Wrapped
function Wrapped(thenable) {
this.val = thenable;
} Wrapped.prototype.unwrap = function () {
return this.val;
}; // wrap
function wrap(thenable) {
return new Wrapped(thenable);
} // unwrap
function unwrap(wrapped) {
return wrapped instanceof Wrapped ? wrapped.unwrap() : wrapped;
} exports.wrap = wrap; exports.unwrap = unwrap;

ECMA 262

https://tc39.es/ecma262/#sec-newpromiseresolvethenablejob

MDN

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/resolve

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/then

refs

https://javascript.info/promise-chaining


class Thenable {
constructor(num) {
this.num = num;
}
then(resolve, reject) {
alert(resolve); // function() { native code }
// resolve with this.num*2 after the 1 second
setTimeout(() => resolve(this.num * 2), 1000); // (**)
}
} new Promise(resolve => resolve(1))
.then(result => {
return new Thenable(result); // (*)
})
.then(alert); // shows 2 after 1000ms

https://masteringjs.io/tutorials/fundamentals/thenable

https://exploringjs.com/es6/ch_promises.html#sec_chaining-promises

https://medium.com/javascript-in-plain-english/whats-the-difference-between-a-thenable-and-a-promise-74d697bc9c79

https://stackoverflow.com/questions/53894038/whats-the-difference-between-resolvethenable-and-resolvenon-thenable-object



xgqfrms 2012-2020

www.cnblogs.com 发布文章使用:只允许注册用户才可以访问!


最新文章

  1. 2Struts2基础----青软S2SH(笔记)
  2. openjudge6047分蛋糕[DP]
  3. 【leetcode】Wildcard Matching(hard) ★ 大神太牛了
  4. 在CentOS 6.6下安装与配置mysql
  5. 请求webservice接口的某方法数据
  6. mysql 函数,关键字,特性
  7. mysql 中间件 分析
  8. Spring 对JDBC的支持(JdbcTemplate)
  9. 解决Access restriction问题
  10. 传统flv网页视频(flvplayer)--兼容
  11. java读取txt文件内容
  12. 【★】致全球第一批全帧3D游戏!
  13. ActiveMQ使用的设计模式
  14. mysql 给表和字段加注释
  15. Android动态添加Device Admin权限
  16. Linux 第三天
  17. maven的传递性依赖
  18. 使用GMap.NET类库,实现地图轨迹回放。(WPF版)
  19. 高性能JavaScript(加载和执行)
  20. JSP中解决中文乱码

热门文章

  1. 02. struts2中Action名称的搜索顺序
  2. postgres多知识点综合案例
  3. Bitter.Core系列二:Bitter ORM NETCORE ORM 全网最粗暴简单易用高性能的 NETCore ORM 之数据库连接
  4. 单点登录(SSO)的设计与实现
  5. 线性DP总结(studying
  6. mysql、sql server、oracle大比较
  7. Excel 如何使用 函数 实现传说中的 多条件中位数 (MEDIANIFS)?
  8. 【wp】HWS计划2021硬件安全冬令营线上选拔赛
  9. windows下hash抓取的几种方式
  10. Docker --Dockerfile(制作镜像)