promise

Return when first promise resolves

你说的曾经没有我的故事 提交于 2021-02-08 11:32:34
问题 Goal I have a bunch of file names in an array, and would like to read the contents of the first of the files that exists. They're config files, so it's important that the order is deterministic, so I can't use .race() . The version I have below maps over each file in order, tries to load it, and if it loads successfully, calls resolve. Problems Here are a couple of issues with this implementation: Calling resolve(...) doesn't actually exit the loop, so the program opens every file in the list

Promise will not resolve

a 夏天 提交于 2021-02-08 11:16:30
问题 The second part of the Promise below (inside the then ) is never run. When I run the database query without using the Promise(in a node script that I run node myscript.js it returns the data but the console never returns the prompt--the console just hangs and I have to send an interrupt manually. Therefore, when I put it inside a Promise, I think the Promise doesn't know that the database query is complete even though it seems to have returned all the data, therefore the second part of the

Promise will not resolve

[亡魂溺海] 提交于 2021-02-08 11:16:12
问题 The second part of the Promise below (inside the then ) is never run. When I run the database query without using the Promise(in a node script that I run node myscript.js it returns the data but the console never returns the prompt--the console just hangs and I have to send an interrupt manually. Therefore, when I put it inside a Promise, I think the Promise doesn't know that the database query is complete even though it seems to have returned all the data, therefore the second part of the

Convert d3.csv to promise?

泄露秘密 提交于 2021-02-08 06:23:26
问题 Using Angular 5 and I would like to load a CSV file into the async pipe, how do I convert this to a promise? d3.csv(this.csvFile, function(data) { console.log(data); }); 回答1: Starting with d3 version 5, promises are built in. d3.csv("file.csv").then(function(data) { console.log(data); }); If you use async/await you can do this: const data = await d3.csv("file.csv"); console.log(data); 回答2: With enough googling I worked it out... loadCSV(file: string) { return new Promise(function (resolve,

why promise can not catch the error throw by setTimeout? [duplicate]

偶尔善良 提交于 2021-02-08 04:47:00
问题 This question already has answers here : Asynchronous exception handling with bluebird promises (3 answers) Closed 2 years ago . why the code below can not catch the error ? the 'catch' do not work and the error throwout and quit. I run the code on nodeJs 8.9 new Promise(function(resolve,reject){ console.log('construct a promise...') setTimeout(() => { throw new Error('async operation failure!') },1000) }) .then(() => { //never happen console.log('happen?wrong!!!') }) .catch(e => { console

why promise can not catch the error throw by setTimeout? [duplicate]

余生长醉 提交于 2021-02-08 04:46:24
问题 This question already has answers here : Asynchronous exception handling with bluebird promises (3 answers) Closed 2 years ago . why the code below can not catch the error ? the 'catch' do not work and the error throwout and quit. I run the code on nodeJs 8.9 new Promise(function(resolve,reject){ console.log('construct a promise...') setTimeout(() => { throw new Error('async operation failure!') },1000) }) .then(() => { //never happen console.log('happen?wrong!!!') }) .catch(e => { console

setTimeout not triggered while using Sinon's fake timers

核能气质少年 提交于 2021-02-08 04:33:30
问题 I have a test similar to what is shown below. Basically I want to test if a specific method is getting delayed. The following example works as expected, i.e. the resolve method gets called and the test passes: it(`should delay execution by 1 second`, function () { const clock = sandbox.useFakeTimers(); const p = new Promise(function (resolve) { setTimeout(resolve, 1000); }); clock.tick(1000); return p; }); However, if I wrap the setTimeout in another Promise, the resolve never gets called: it

setTimeout not triggered while using Sinon's fake timers

 ̄綄美尐妖づ 提交于 2021-02-08 04:32:10
问题 I have a test similar to what is shown below. Basically I want to test if a specific method is getting delayed. The following example works as expected, i.e. the resolve method gets called and the test passes: it(`should delay execution by 1 second`, function () { const clock = sandbox.useFakeTimers(); const p = new Promise(function (resolve) { setTimeout(resolve, 1000); }); clock.tick(1000); return p; }); However, if I wrap the setTimeout in another Promise, the resolve never gets called: it

Async Generator: Yielding a rejected promise

◇◆丶佛笑我妖孽 提交于 2021-02-07 21:54:23
问题 I've been playing around with async generators in an attempt to make a "promise ordering" generator which takes an array of promises and yields out promises one by one in the order they resolve or reject. So something like: async function* orderProms(prom_arr) { // Make a copy so the splices don't mess it up. const proms = [...prom_arr]; while (proms.length) { // Tag each promise with it's index, so that we can remove it for the next loop. const {prom, index} = await Promise.race(proms.map(

Angular custom error handler not getting error type from promise

自古美人都是妖i 提交于 2021-02-07 18:48:19
问题 When thrown from a promise every Error my custom error handler is getting does loose its type import { HttpErrorResponse } from "@angular/common/http"; import { ErrorHandler, Injectable, Injector, NgZone } from "@angular/core"; import { MatSnackBar } from "@angular/material"; @Injectable() export class GlobalErrorHandler implements ErrorHandler { constructor(private injector: Injector) { } handleError(error: any): void { if (error instanceof HttpErrorResponse) // this needs to be triggered