Skip to content

Latest commit

 

History

History
22 lines (19 loc) · 606 Bytes

use-promises-for-cleaner-asynchronous-logic.md

File metadata and controls

22 lines (19 loc) · 606 Bytes

使用promise模式清洁异步逻辑

// 使用ES6的 Promise
    let p = new Promise((resolve, reject) => {
                setTimeout(() => {
                    resolve('hello')
                }, 2000);
            })
    p.then((res) => {
        console.log(res);
    })

源码


谨记

  • promise代表最终值,即并行操作完成时最终产生的结果。
  • 使用promise组合不同的并行操作。
  • 使用promise模式的API避免数据竞争。
  • 在要求有意的竞争条件时使用select(也称为choose)