推荐 最新
sumous_01

js中for与map在返回promise时的区别?

遇到一个问题:下面是拿到一个数组返回,通过遍历返回一个值,我之前习惯用map去遍历。 const getCapture=async ()=>{ let result=await ipcRenderer.invoke('goCapture-event') for(const m of result){ if(m.name==='整个屏幕'){ let str=m.thumbnail.crop({x:0,y:0,width:1200,height:1170}) const imgSrc=str.toDataURL() return imgSrc } } /* result.map(m=>{ if(m.name==='整个屏幕'){ console.log(m) let str=m.thumbnail.crop({x:0,y:0,width:1000,height:1000}) const imgStr=str.toDataURL() return imgStr } }) */ } 这是map的遍历: result.map(m=>{ if(m.name==='整个屏幕'){ console.log(m) let str=m.thumbnail.crop({x:0,y:0,width:1000,height:1000}) const imgStr=str.toDataURL() return imgStr } }) 这样返回的竟然是空, 但是如果用for,就可以正确返回: for(const m of result){ if(m.name==='整个屏幕'){ let str=m.thumbnail.crop({x:0,y:0,width:1200,height:1170}) const imgSrc=str.toDataURL() return imgSrc } } js中for和map还有这区别吗?

0
1
0
浏览量196