草庐IT

promise2

全部标签

javascript - Promise.all 返回一个未定义的数组

我在返回未定义数组的函数时遇到问题。代码如下:classMethods.getQueries=function(models,dbId,dateStart,dateEnd){returnnewPromise(function(resolve,reject){/*Fetchdatabase....*/.then(extractQueries,reject).then(sortQueries,reject).then(onlyTen,reject).then(addText,reject).then(function(queries){console.log('getQueriesfini

javascript - ES6 Promise.all() 错误句柄 - 是否需要 .settle()?

这个问题在这里已经有了答案:Waituntilallpromisescompleteevenifsomerejected(20个回答)6年前关闭。假设我有一个处理两个promise的Promise.all()。如果一个promise产生错误,但另一个promise解决了,我希望能够在Promise.all()解决后根据情况处理错误。ES6Promises缺少解决方法,我假设是有充分理由的。但我不禁认为.settle()方法会让我更容易解决这个问题。我是以错误的方式解决这个问题,还是用一种解决方法扩展ES6Promise是正确的做法?我如何考虑使用.settle()的一个例子:Promi

javascript - 在 Promise 中,使用 catch 和 then 的第二个参数有什么区别?

这个问题在这里已经有了答案:Whenis.then(success,fail)consideredanantipatternforpromises?(7个答案)关闭6年前。这两种说法到底有什么区别?funcThatReturnsAPromise().then(()=>{/*success*/}).catch(()=>{/*fail*/});funcThatReturnsAPromise().then(()=>{/*success*/},()=>{/*fail*/});

javascript - 使用 node.js、流和 promise 下载文件

这是我的代码片段:varprocessListing=function(directoryItems){console.log('foreach');varitemsToDownload=[];directoryItems.forEach(function(element,index,array){//Ignoredirectoriesif(element.type==='d'){console.log('directory'+element.name);return;}//Ignorenonzipsif(path.extname(element.name)!=='.zip'){con

javascript - Async/Await with Request-Promise 返回 Undefined

我有两个文件;server.js和scrape.js,下面是它们当前的代码片段。服务器.js:constscrape=require("./scrape");asyncfunctionstart(){constresponse=awaitscrape.start();console.log(response);}start();和scrape.js:constcheerio=require("cheerio");constrequest=require("request-promise");go=async()=>{constoptions={uri:"http://www.somew

javascript - 处理错误后跳过 promise 链

使用https://github.com/kriskowal/q图书馆,我想知道是否有可能做这样的事情://ModuleAfunctionmoduleA_exportedFunction(){returnpromiseReturningService().then(function(serviceResults){if(serviceResults.areGood){//Wecancontinuewiththerestofthepromisechain}else{performVerySpecificErrorHandling();//Wewanttoskiptherestofthep

javascript - JavaScript Promise 是异步的吗?

只是一个快速澄清的问题:JavaScriptPromise是异步的吗?我已经阅读了很多关于Promise和异步编程(即ajax请求)的文章。如果Promise不是异步的,我们如何做到这一点?例如,我有一个函数可以将带有参数数组args的函数f包装在Promise中。f本身就不是异步的。functiongetPromise(f,args){returnnewPromise(function(resolve,reject){varresult=f.apply(undefined,args);resolve(result);});}为了实现这种异步,我阅读了一些SO帖子并决定setTimeo

javascript - 错误 : Uncaught (in promise): Error: Cannot match any routes RC4 Child Routes

在Angular应用程序中实现子路由的演示应用程序Angular2应用程序显示错误Error:Uncaught(inpromise):Error:Cannotmatchanyroutes:'movie-home'zone.js:461UnhandledPromiserejection:Cannotmatchanyroutes:'movie-home';Zone:angular;Task:Promise.then;Value:Error:Cannotmatchanyroutes:'movie-home'(…)如果我不从文件movie.routes.ts添加这些代码行,应用程序工作正常{p

javascript - 串联执行原生js promise

我必须为数组中的每个项目调用一些异步任务的promise,但我想连续执行这些任务。Promise.all仅在拥有一个合并promise列表但不按顺序调用它们的新promise时有用。我如何使用标准的promiseapi来实现这一点,而无需第三方库,如Q、bluebird.... 最佳答案 您使用.then()链式promise带有返回另一个promise的回调。因此,假设您有三个函数a、b和c,它们都返回一个promise。您可以像这样链接它们(按顺序执行):a().then(b).then(c).then(function(res

javascript - 在 node.js 中使用带有流的 promise

我重构了一个简单的实用程序来使用promise。它从网上获取pdf并将其保存到磁盘。一旦保存到磁盘,它应该在pdf查看器中打开文件。该文件出现在磁盘上并且有效,shell命令打开OSX预览应用程序,但会弹出一个对话框,提示该文件为空。一旦文件流写入磁盘,执行shell函数的最佳方式是什么?//downloadapdfandsavetodisk//openpdfinosxpreviewforexampledownload_pdf().then(function(path){shell.exec('open'+path).code!==0);});functiondownload_pdf(