博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
LazyMan的Promise解法
阅读量:4454 次
发布时间:2019-06-07

本文共 4829 字,大约阅读时间需要 16 分钟。

背景

见上一篇。 面向对象的链式调用中,掺杂了 一个一部动作, 对于这种工作链, 是非同步执行的链。

 

LazyMan("Hank").sleep(1).eat("dinner")

 

同步执行的工作链中, 任何一个动作,即函数调用, 都是同步的, 可理解为普通的函数。

异步的工作链, 前提条件是工作链中,存在至少一个  动作是 异步的。 例如 sleep

 

Promise

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

The Promise object is used for asynchronous computations. A Promise represents a value which may be available now, or in the future, or never.

 

A Promise is a proxy for a value not necessarily known when the promise is created. It allows you to associate handlers to an asynchronous action's eventual success value or failure reason. This lets asynchronous methods return values like synchronous methods: instead of the final value, the asynchronous method returns a promise for the value at some point in the future.

 

A Promise is in one of these states:

  • pending: initial state, not fulfilled or rejected.
  • fulfilled: meaning that the operation completed successfully.
  • rejected: meaning that the operation failed.

A pending promise can either be fulfilled with a value, or rejected with a reason (error). When either of these happens, the associated handlers queued up by a promise's then method are called.

 

 

四大特性:

 

then特性:

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

The then() method returns a . It takes up to two arguments: callback functions for the success and failure cases of the Promise.

 

Syntax

p.then(onFulfilled[, onRejected]);p.then(function(value) {   // fulfillment  }, function(reason) {  // rejection});

Parameters

then returns a Promise which is determined by the input functions:

  • If onFulfilled or onRejected throws an error, or returns a Promise which rejects, then returns a rejected Promise.
  • If onFulfilled or onRejected returns a Promise which resolves, or returns any other value, then returns a resolved Promise.

 

Chaining

The then method returns a Promise which allows for method chaining.

You can pass a lambda to then and if it returns a promise, an equivalent Promise will be exposed to the subsequent then in the method chain. The below snippet simulates asynchronous code with the setTimout function.

 

If onFulfilled returns a promise, the return value of then will be resolved/rejected by the promise.

 

function resolveLater(resolve, reject) {  setTimeout(function () {    resolve(10);  }, 1000);}function rejectLater(resolve, reject) {  setTimeout(function () {    reject(20);  }, 1000);}var p1 = Promise.resolve("foo");var p2 = p1.then(function() {  // Return promise here, that will be resolved to 10 after 1 second  return new Promise(resolveLater);});p2.then(function(v) {  console.log("resolved", v);  // "resolved", 10}, function(e) {  // not called  console.log("rejected", e);});var p3 = p1.then(function() {  // Return promise here, that will be rejected with 20 after 1 second  return new Promise(rejectLater);});p3.then(function(v) {  // not called  console.log("resolved", v);}, function(e) {  console.log("rejected", e); // "rejected", 20});

 

 

解法

 结合 then的链使用, 给出方案:

function _LazyMan(name) {    this.promiseGetters = [];    var makePromise = function  () {        var promiseObj = new Promise(function(resolve, reject){            console.log("Hi! This is " + name + "!");            resolve();        })        return promiseObj;    }    this.promiseGetters.push(makePromise);    // 在各个Promise的then函数中,将任务序列穿起来    var self = this;    var sequence = Promise.resolve();    // Promise.resolve 等价于    // var sequence = new Promise(function (resolve, reject) {
// resolve(); // }) setTimeout(function(){ for (var i = 0; i < self.promiseGetters.length; i++) { var nowPromiseGetter = self.promiseGetters[i]; var thenFunc = (function (nowPromiseGetter) { return function () { return nowPromiseGetter() } })(nowPromiseGetter); sequence = sequence.then(thenFunc); }; }, 0); // 在下一个事件循环启动任务}_LazyMan.prototype.eat = function(name) { var makePromise = function () { var promiseObj = new Promise(function(resolve, reject){ console.log("Eat " + name + "~"); resolve(); }) return promiseObj; } this.promiseGetters.push(makePromise); return this; // 实现链式调用}_LazyMan.prototype.sleep = function(time) { var makePromise = function () { var promiseObj = new Promise(function(resolve, reject){ setTimeout(function(){ console.log("Wake up after " + time + "s!"); resolve(); }, time * 1000); }) return promiseObj; } this.promiseGetters.push(makePromise); return this;}/* 封装 */function LazyMan(name){ return new _LazyMan(name);}LazyMan("Hank").sleep(1).eat("dinner")

 

转载于:https://www.cnblogs.com/lightsong/p/6260502.html

你可能感兴趣的文章
windows 游戏编程大师 读书笔记
查看>>
avalon.js中使用owl-carousel轮播图
查看>>
phpcms笔记
查看>>
今天第一天,思考
查看>>
图层时间之层级关系时间
查看>>
常见算法之0---冒泡排序
查看>>
Spring boot 默认首页配置
查看>>
host的作用
查看>>
为什么operator<<运算符重载一定要为友元函数
查看>>
jsonp跨域
查看>>
再温习JAVA命名规范
查看>>
libevent学习过程
查看>>
webview加载页面为什么在UI线程里面做,难道不是耗时操作么
查看>>
TensorFlow 安装 Ubuntu14.04
查看>>
springmvc 注解 RequestParam/RequestHeader/CookieValue
查看>>
20175310 《Java程序设计》第1周学习总结(1)安装虚拟机
查看>>
2016012016+小学四则运算练习软件项目报告
查看>>
Java入门系列(一)基础概览
查看>>
团队编程项目作业2-团队编程项目代码设计规范
查看>>
c# aspose 模版 简单导出execl
查看>>