function sleep(ms) { return new Promise((resolve) => { $.ajax({ url:"http://crawlerapi.buji.net.cn/api/Crawler/projects?pageIndex=1&pageSize=10", async:true, success:function(result){ resolve(result.success+"++"+ms) }}); }); } async function asyncFunction() { console.time('asyncFunction total executing:'); const sleep1 = await sleep(2000); console.log('sleep1: ' + sleep1); const [sleep2, sleep3, sleep4]= await Promise.all([sleep(2000), sleep(1000), sleep(1500)]); console.log('sleep2: ' + sleep2); console.log('sleep3: ' + sleep3); console.log('sleep4: ' + sleep4); const sleepRace = await Promise.race([sleep(3000), sleep(1000), sleep(1000)]); console.log('sleep race: ' + sleepRace); console.timeEnd('asyncFunction total executing:'); return 'asyncFunction done.' // 这个可以不返回,这里只是做个标记,为了显示流程 } asyncFunction()
await
最新推荐文章于 2024-08-16 11:35:39 发布