• Promise()


    Promise()

      The constructor is primarily used to wrap functions that do not already support promises.

      

    executor

      A function that is passed with the arguments resolve and reject. The executor function is executed immediately by the Promise implementation, passing resolve and reject functions (the executor is called before the Promiseconstructor even returns the created object). 

      红字部分:executor在new Promise()返回前就被执行。

      

    Promise is in one of these states:

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

    Promise.all(iterable)

      Returns a promise that either fulfills when all of the promises in the iterable argument have fulfilled or rejects as soon as one of the promises in the iterable argument rejects. If the returned promise fulfills, it is fulfilled with an array of the values from the fulfilled promises in the same order as defined in the iterable. If the returned promise rejects, it is rejected with the reason from the first promise in the iterable that rejected. This method can be useful for aggregating results of multiple promises.

      全部异步promise结束,或其中一个出错。

      为了让一个异步调用有 Promise能力,返回一个new Promise()即可。

    function myAsyncFunction(url) {
      return new Promise((resolve, reject) => {
        const xhr = new XMLHttpRequest();
        xhr.open("GET", url);
        xhr.onload = () => resolve(xhr.responseText);
        xhr.onerror = () => reject(xhr.statusText);
        xhr.send();
      });
    };
    View Code

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

  • 相关阅读:
    01、u-boot 打补丁 编译 烧写
    3、JZ2440 按键驱动(中断)
    2、JZ2440 按键驱动(查询)
    1、JZ2440 LED驱动
    004、栈的基本操作
    003、动态顺序表的插入与删除
    002、静态顺序表的插入与删除
    对百度的评价
    关于找水王的思路
    软件市场应用前景
  • 原文地址:https://www.cnblogs.com/tekkaman/p/7496938.html
Copyright © 2020-2023  润新知