首页 > 其他分享 >promise 并发请求-Promise.all()/Promise.allSettle()

promise 并发请求-Promise.all()/Promise.allSettle()

时间:2023-08-25 13:55:05浏览次数:32  
标签:callback promise allSettle params error Promise requests

方法定义

//request.js 定义
export function all(requests, callback) {
  Promise.all(requests)
    .then(params => callback(params))
    .catch(error => {
      console.error(error)
    })
}
export function allSettle(requests, callback) {
  Promise.allSettled(requests)
    .then(params => callback(params))
    .catch(error => {
      console.error(error)
    })
}

方法使用


import {  allSettle } from '@/utils/request'

export function dashboardData(params, callback) {
  let requests = [
    // post/get 请求组成的promise 数组
  ]
  allSettle(requests, callback)
  // all(requests, callback)
}

方法区别

  • Promise.all 方法请求中所有结果都成功则返回有结果组成的数组,其中一个失败立马返回失败,不继续执行
  • Promise.allSettled 方法执行完数组中所有方法 统一返回结果,不管成功或者失败都会返回。
//状态值 fulfilled(成功),rejected(失败)
[{status:'fulfilled',value:{data:接口返回的值}}] 

标签:callback,promise,allSettle,params,error,Promise,requests
From: https://www.cnblogs.com/zhu-xl/p/17656701.html

相关文章

  • js 异步改成同步Promise
    functionPromise(executor){letself=this;if(typeofexecutor!=='function')thrownewTypeError('Promiseresolver'+executor+'isnotafunction');if(!(selfinstanceofPromise))thrownewTypeError(�......
  • 手写Promise
    1.promise是手写异步代码的另一种方式,主要用于解决回调嵌套问题2.promise提供两个参数resolve(成功时调用的函数),reject(失败时调用的参数),它们是promise内部实现好的函数3.promise有三种状态,pending等待,fulfilled成功,rejected失败4.resolve时,将promise的状态从pending改为fulfi......
  • 异步编程:promise and future
    本文介绍C++中异步编程相关的基础操作类,以及借鉴promiseandfuture思想解决回调地狱介绍。std::threadandstd::jthreadstd::thread为C++11引入,一个简单的例子如下:classWorkerfinal{public:voidExecute(){std::cout<<__FUNCTION__<<std::endl;}......
  • SyntaxError: /xxxx.vue: Unexpected token, expected “,“,[object Promise]export {
    本地老工程vue2.7.x+webpack4在升级webpack5的时候遇启动和打包报错:SyntaxError:SyntaxError:/xxxxx.vueUnexpectedtoken,expected","(1:8)>1|[objectPromise]|^2|export{render,staticRenderFns}最后才发现是prettier导致的。推荐看......
  • Promise的理解和使用
    一:Promise是什么?(1)Promise是JS中进行异步编程的解决方案备注:旧方案是单纯使用回调函数异步编程包括:fs文件操作、数据库操作、AJAX、定时器......(2)从语法上来说:Promise是一个构造函数(3)从功能上来说:Promise对象用来封装一个异步操作并可以获取其成功/失败的结果值二......
  • vue语法错误 + Promise错误 + js 错误,通过钉钉报警
      一、背景:为了使系统更加稳定,在用户使用期间,若发现异常,可及时应对,采取了“报警机制”。通常“报警机制”分为2种,一种是后端对api监控及自定义监控,出现异常,通过钉钉或邮件的形式通知,第二种是前端对js语法,vue语法,自定义报错进行监控,以此来规范代码质量,保证系统预警二、流程......
  • 关于Promise的超难面试题解读
    让我来看一下题目,如下所示Promise.resolve().then(()=>{ console.log(0); returnPromise.resolve(4); }).then((res)=>{ console.log(res); }); Promise.resolve().then(()=>{ console.log(1); }).then(()=>{ console.log(2); }).t......
  • 【JavaScript30】promise
    在前端js中是可以发送网络请求的,如果前端js的请求是线性的请求(同步),网站的体验会很差。设计js发请求的那个人.选择了使用异步执行方式.大幅度的提升用户体验.console.log("我要发请求了");setTimeout(function(){console.log("服务器返回结果了");},2000);//假设......
  • 重学JavaScript Promise API
    在这篇教程中,我们将掌握如何在JavaScript中创建并使用Promise。我们将了解Promise链式调用、错误处理以及最近添加到语言中的一些Promise静态方法。什么是Promise?在JavaScript中,一些操作是异步的。这意味着当这些操作完成时,它们产出的结果或者值并不会立即生效。Promise是一......
  • 前端学习笔记202306学习笔记第四十二天-promise-then的返回值3
       ......