promise

Why is my chained promise blocking?

烈酒焚心 提交于 2021-02-04 14:01:30
问题 I have a class method that chains together other methods in the class, and also calls a method on another instance of the class: class Thing { doSomething(nextThing) { return new Promise((resolve) => this.initialize() .then(() => this.doA()) .then(() => { nextThing.initialize(); // call initialize() on another instance return this.doB(); }) .then(() => this.doC()) .then(resolve) ); } initialize() { return new Promise((resolve) => { // take a long time to do something // ... // ... resolve();

Typescript Promise rejection type

孤街浪徒 提交于 2021-02-04 12:50:07
问题 How do I set the type of the rejection of my promise? Let's say I do: const start = (): Promise<string> => { return new Promise((resolve, reject) => { if (someCondition) { resolve('correct!'); } else { reject(-1); } }); } Let's say I want to reject with a number. But I cannot set the type; I can pass whatever I want to the reject here. Moreover, when using this promise, I want to have compiling error if I use the rejection response type incorrectly. 回答1: As explained in this issue, Promise

Why is my function returning Promise { <pending> } [duplicate]

落花浮王杯 提交于 2021-02-04 08:41:25
问题 This question already has answers here : Async function returning promise, instead of value (3 answers) Closed 5 months ago . As my first real MERN project, I'm building a message board. I'm currently working on a node route to request the board names with their associated post count but I've hit an issue. Instead of getting the values that I need, I'm recieving info telling me there is a promise pending, which seems odd as I'm using async/await. Here's the function: exports.postsPerBoard =

Return value from a Promise constructor

眉间皱痕 提交于 2021-02-01 18:08:17
问题 Consider two examples below... TEST 1 function test1() { return new Promise(function () { return 123; }); } test1() .then(function (data) { console.log("DATA:", data); return 456; }) .then(function (value) { console.log("VALUE:", value); }); It outputs nothing. TEST 2 function test2() { return new Promise(function (resolve, reject) { resolve(123); }); } test2() .then(function (data) { console.log("DATA:", data); return 456; }) .then(function (value) { console.log("VALUE:", value); }); It

Return value from a Promise constructor

南笙酒味 提交于 2021-02-01 18:01:35
问题 Consider two examples below... TEST 1 function test1() { return new Promise(function () { return 123; }); } test1() .then(function (data) { console.log("DATA:", data); return 456; }) .then(function (value) { console.log("VALUE:", value); }); It outputs nothing. TEST 2 function test2() { return new Promise(function (resolve, reject) { resolve(123); }); } test2() .then(function (data) { console.log("DATA:", data); return 456; }) .then(function (value) { console.log("VALUE:", value); }); It

Return value from a Promise constructor

感情迁移 提交于 2021-02-01 18:01:34
问题 Consider two examples below... TEST 1 function test1() { return new Promise(function () { return 123; }); } test1() .then(function (data) { console.log("DATA:", data); return 456; }) .then(function (value) { console.log("VALUE:", value); }); It outputs nothing. TEST 2 function test2() { return new Promise(function (resolve, reject) { resolve(123); }); } test2() .then(function (data) { console.log("DATA:", data); return 456; }) .then(function (value) { console.log("VALUE:", value); }); It

What happen when we return a value and when we return a Promise.resolve from a then() chain, in the microtask queue?

非 Y 不嫁゛ 提交于 2021-01-29 19:17:53
问题 Here I have a promise that simply resolved. let promise = new Promise((resolve,reject)=>{ resolve("resolved"); }); The confusion for me starts when I use a Promise for return value in .then chain like this: promise.then(resolve=>{ console.log(resolve); return Promise.resolve(2); }).then(resolve=>{ console.log(resolve); }); promise.then(resolve=>{ console.log(resolve) return 3; }).then(resolve=>{ console.log(resolve); }); The output of these chains is: 1 1 3 2 Which I expected to see: 1 1 2 3

promise.then functions only works if defined inside

你说的曾经没有我的故事 提交于 2021-01-29 12:10:39
问题 There is something that I dont really understand from promises. It used to happen to me with callbacks too. I don't know what I'm not seeing. When I define a function inside "promise.then", it works correctly. But when I define the same function with the same parameter outside, it says that the parameter isn't defined. What's happening here? There is another way to have a cleaner code? Im posting a code that uses express and axios npm, but I don't think that's a problem. app.get('/', function

Async await not work properly inside map function

空扰寡人 提交于 2021-01-29 11:51:20
问题 I have a Dataset Structure like this: _id: SomeMongoID, value: "a", counter: 1 } So initially my database table is empty. Now I have an array where value is like: const array = ["a", "a", "a"] What I want is initially the first time I do the search, so it will empty result so at that case in insert the query, now next time it get the entry so simply increment the counter. For that, I wrote the code: const testFunction = async(array) => { try { await Promise.all( array.map(async x => { const

How to control setTimeout with promises

旧巷老猫 提交于 2021-01-29 11:39:38
问题 var functionsArray = [ function() { setTimeout(function() { console.log(1); }, 100); }, function() { setTimeout(function() { console.log(2); }, 200); }, function() { setTimeout(function() { console.log(3); }, 10); } ], Say I have an array of functions like above(the number of functions in it is not known). I want to write a function which takes this array as parameter and executes them in sequence. In the example above, I want it to log 1,2,3 in the sequence. Since Promise.all does not