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"

     

  • 相关阅读:
    15-数组concat()方法和push比较
    06-使用云储存上传工具
    05-云函数/云数据库的增删改查
    错题1
    c++链表
    8817
    8816
    1177
    1355
    c++期末考
  • 原文地址:https://www.cnblogs.com/eret9616/p/9021186.html
Copyright © 2011-2022 走看看