首页 > 其他分享 >[ES2024] Manually settle a promise using Promise.withResolvers

[ES2024] Manually settle a promise using Promise.withResolvers

时间:2024-05-22 14:44:41浏览次数:25  
标签:resolve Manually res ES2024 settle Promise reject promise withResolvers

If we want to be able to modify the state of a promise from outside the constructor, we can use the Promise.withResolvers method to get access to the promise, and its resolve and reject functions.

 

// old approach
let res, rej;

const promise = new Promise((resolve, reject) => {
    res = resolve;
    rej = reject;
})

onClick(res)

 

// New approach

const { promise, resolve, reject } = Promise.withResolvers()

onClick(resolve)

 

标签:resolve,Manually,res,ES2024,settle,Promise,reject,promise,withResolvers
From: https://www.cnblogs.com/Answer1215/p/18206180

相关文章

  • [ES2024] Improve Application-wide Error Handling rethrowing JavaScript Error wit
    Thenew cause datapropertythatyoucanaddtoathrown Error canbeusedtoretainaccesstotheoriginalerrorcaughtinapromiserejection. constsendLog=(...args)=>console.log(...args);asyncfunctionfetchStuff(){awaitfetch('h......
  • [ES2024] Simplify array immutable changes with the new array.with method
    Thenew Array.with methodgivesyouanimmutablesyntaxforchangingvaluesofanarrayataspecifiedindex.Sometimes .map willbemoreefficient.So,inthislessonwe'llcomparebothmethodswhilereplacinganobjectataspecificindex. varto......
  • WPF C# construct Grid,DataGrid,Button manually
    usingMicrosoft.Win32;usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Windows;usingSystem.Windows.Controls;usingSystem.Windows.Data;usingSystem.Windows.Documents;usin......
  • 除了Promise.all(),使用Promise.allSettled()方式请求,避免使用循环请求
    constgetFilePromises:Promise<any>[]=[];fileIds.forEach((item)=>{getFilePromises.push(getFileInfoApi({id:item}));});Promise.allSettled(getFilePromises).then((res)=>{this.fileList=res.map((item,index)=>......
  • OEM DBConsole manually
    Step1.Droptheexistingconfigurationifitishavingsysmanuseralreadypresent.SQL>Selectusernamefromdba_userswhereusername='SYSMAN';USERNAME------------------------------SYSMANThebelowcommandtodroptheexistingconfigu......
  • Codeforces Round 857 (Div. 2) B. Settlement of Guinea Pigs
    你非常喜欢豚鼠。每个笼子可以住两只豚鼠,且你不想把每个笼子放入不同性别的豚鼠。豚鼠只有两种性别。假设你买到豚鼠时并不知道性别,只有医生能够分辨。接下来的\(n\)天方案中,若第\(i\)天为\(1\),你买入一只豚鼠;若为\(2\),你请医生分辨你的豚鼠性别。给出方案,你最少需要准......
  • promise 并发请求-Promise.all()/Promise.allSettle()
    方法定义//request.js定义exportfunctionall(requests,callback){Promise.all(requests).then(params=>callback(params)).catch(error=>{console.error(error)})}exportfunctionallSettle(requests,callback){Promise.allSettl......
  • 如何在JavaScript中使用Promise.allSettled()
    您是否曾经在JavaScript中使用过Promise,并且当有人拒绝并毁掉一切时感到沮丧?你编写了一些基于Promise的代码,一切都进展顺利,然后繁荣——一个小小的Promise被拒绝,整个链条就会崩溃。你的代码逐渐停止,你想知道为什么JavaScript不能忽略这个小问题并继续它的快乐之路。好......
  • Definition of 'Cash Settlement( versus physical delivery of the reference obliga
    Definitionof'CashSettlement(versus physicaldeliveryofthereferenceobligation)Asettlementmethodusedincertainfutureandoptioncontractswhereby,uponexpiryorexercise,thesellerofthefinancialinstrumentdoesnotdelivertheactual......
  • Settle Finance:以区块链技术为核心的P2P借贷模式
    "在数字化世界中,我们看到了许多技术进步如何推动金融行业的革新。其中,Peer-to-Peer(P2P)借贷模式的出现为借贷市场带来了新的可能性。然而,这个模式也带来了诸多问题,尤其是在信任和安全性方面。随着区块链技术的兴起,我们看到了它在改善P2P模式中的这些问题上的巨大潜力。区块链的......