zoukankan      html  css  js  c++  java
  • Promise

    20181101补充:
    Promise A+规范:
    https://promisesaplus.com/
     
    Promise的语法:
    new Promise(resolve,reject){
     
       。。。。执行代码
       
    }.then(onFulfilled, onRejected)
     
     
     
    let promiseToCleanTheRoom = new Promise(function(resolve,reject){


    //cleaning the room

    let isClean = false;

    if(isClean){
    resolve('Clean');
    }else{
    reject('not Clean');
    }

    });

    promiseToCleanTheRoom.then(function(fromResolve){

    console.log('the room is' + fromResolve);
     
    }).catch(function(fromReject){

    console.log('the room is' + fromReject);
    });
     
     

    resolve:

    reject:

    three seperate Promises:

    Promise.all[ xxx,xxx,xxx]:(xxx为promise对象) 

    Promise.all(iterable) 方法返回一个 Promise 实例,此实例在 iterable 参数内所有的 promise 都“完成(resolved)”或参数中不包含 promise 时回调完成(resolve);如果参数中  promise 有一个失败(rejected),此实例回调失败(rejecte),失败原因的是第一个失败 promise 的结果。

    Promise.race:

    Promise.race(iterable) 方法返回一个 promise ,并伴随着 promise对象解决的返回值或拒绝的错误原因, 只要 iterable 中有一个 promise 对象"解决(resolve)"或"拒绝(reject)"。

    var promise1 = new Promise(function(resolve, reject) {
    setTimeout(resolve, 500, 'one');
    });

    var promise2 = new Promise(function(resolve, reject) {
    setTimeout(resolve, 100, 'two');
    });

    Promise.race([promise1, promise2]).then(function(value) {
    console.log(value);
    // Both resolve, but promise2 is faster
    });
    // expected output: "two"

     

  • 相关阅读:
    P31 整体更新或新增 PUT
    P30 整体更新/替换资源 PUT
    P29 自定义错误信息和错误报告
    python: openpyxl带格式复制excel
    Android控件与布局——基础控件RadioButton
    EditText 使用详解
    Linux内核内存检测工具KASAN
    ISP基础(10)-Gamma校正及其实现
    ISP基础(08)-动态范围压缩
    ISP基础(07)-自动曝光
  • 原文地址:https://www.cnblogs.com/eret9616/p/9021186.html
Copyright © 2011-2022 走看看