promise

  • why promise?
    • 异步编程为js带来强大灵活性的同时,也带来了嵌套回调的问题。详细来说主要有两点,第一嵌套太深代码可读性太差,第二并行逻辑必须串行执行。
    • 新的ES6中引入了Promise,目的是让回调更为优雅。层层嵌套的回调会让javascript失去美感和可读性,同时javascript也推荐采用链式的方式去书写函数调用。
  • PromiseA+规范
    • A promise represents the eventual result of an asynchronous operation.
      一个promise代表了一个异步操作的最终结果
    • The primary way of interacting with a promise is through its then method, which registers callbacks to receive either a promise’s eventual value or the reason why the promise cannot be fulfilled.跟promise交互的主要方式是通过他的then方法来注册回调函数去接收promise的最终结果值或者是promise不能完成的原因。
  • how to create promise?
  • how to convert a function to promise?
    • by returning a new Promise
    • the promise define how the resolve & reject function to be used
  • what does the promise chain mean?
    • .then() return a promise
      • which kinds of promise?
      • what about the original function to be run?
  • how to handle the parallel event?
    • use promise.all()
  • api
    • Promise.resolve()
    • Promise.reject()
    • Promise.prototype.then()
    • Promise.prototype.catch()
    • Promise.all() // 所有的完成
       var p = Promise.all([p1,p2,p3]);
    • Promise.race() // 竞速,完成一个即可
    • join???
    • how is the above used? similar to .then()?
  • url

 

Advertisements
This entry was posted in Data Model, JavaScript, Nodejs. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s