es6-promise

javascript return new Promise: short syntax

拜拜、爱过 提交于 2019-11-28 14:16:09
When working with asynchronious javascript every second function looks like this: function_name() { return new Promise((resolve,reject) => { // do some async task resolve(); }); } Even with es6 async/await I cannot avoid the part "return new Promise((resolve,reject) => { ...". Is there another way of writing such code without all those duplicated lines? Thanks. First off, you should be avoiding the promise anti-pattern that wraps a new promise around other functions that already return promises. If you're doing that, then you can just stop doing that entirely and just return the promise that

Why is my infinite loop blocking when it is in an async function? [duplicate]

末鹿安然 提交于 2019-11-28 13:11:05
This question already has an answer here: Proper way to write nonbloking function in node.js 1 answer I don't understand how to work with asynchronous functions. Why does the code below stop the main thread? async function foo() { for (;;) {} } foo(); The async keyword doesn't make synchronous code asynchronous, slow running code fast, or blocking code non-blocking. It just makes the function return a promise and provides (with the await keyword) a mechanism to interact with other promises as if there were synchronous. Your function starts a loop, and then just goes around and around. It doesn

Mocha exceeding 2000ms timeout when returning a promise [duplicate]

▼魔方 西西 提交于 2019-11-28 12:06:54
问题 This question already has answers here : Change default timeout for mocha (4 answers) Closed 2 years ago . I'm trying to test my Express.js controllers but every so often I run into a problem with the mocha timeout telling me off. The docs (https://mochajs.org/#working-with-promises) and the answer here: https://stackoverflow.com/a/26572442/1646372 state that I can just return the promise I'm using. I've wrapped my express controllers with Promises so that I can then return them in the tests.

Weird behavior with Promise throwing “Unhandled promise rejection” error

耗尽温柔 提交于 2019-11-28 11:45:00
When I run this code using Node, it throws an Unhandled promise rejection error in the console (even showing the error caught text first). const promise = new Promise((resolve, reject) => setTimeout(reject, 1000)) promise.then(() => console.log('ok')) promise.catch((e) => console.log('error caught')) Nevertheless, when I chain the catch method to the then method, the error disappears: const promise = new Promise((resolve, reject) => setTimeout(reject, 1000)) promise.then(() => console.log('ok')).catch((e) => console.log('error caught')) Isn't the first code supposed to handle the rejection? I

Get first fulfilled promise

痴心易碎 提交于 2019-11-28 11:34:33
If I have two promises A and B, only one of which will succeed, how can I get whichever one fulfills successfully? I'm looking for something similar to Promise.race , but which will return only the first promise that fulfills. I'm using promises from ES6. Invert the polarity of the promises, and then you can use Promise.all , because it rejects on the first rejected promise, which after inversion corresponds to the first fulfilled promise: const invert = p => new Promise((res, rej) => p.then(rej, res)); const firstOf = ps => invert(Promise.all(ps.map(invert))); // Utility routines used only in

Extending Promises in ES6

此生再无相见时 提交于 2019-11-28 10:44:56
I am trying to extend Promise: class PersistedPromise extends Promise { } Then call the static resolve on the derived class to directly create a resolved promise: PersistedPromise.resolve(1) In traceur, this yields: ModuleEvaluationError: #<PersistedPromise> is not a promise at new PersistedPromise (~rtm/gen/promise.js:6:57) at Function.resolve (native) In Babel (run as babel-node --experimental promise.js ) it results in: Promise.apply(this, arguments); ^ TypeError: [object Object] is not a promise at new PersistedPromise (~rtm/gen/promise.js:1:23) at Function.resolve (native) ... I was

In javascript, a function which returns promise and retries the inner async process best practice

丶灬走出姿态 提交于 2019-11-28 10:31:31
I have a function which returns a javascript Promise and inside it there runs some asynchronously code. The async code, needs to be retried for couple of times in cases it fails. I was doing that, till I observed some strange behaviors which made me wonder if I'm doing it right. So I had to change it. Both approaches snipped are down here. I have some idea why the first approach (asyncFunc) doesn't work, I would appreciate if someone could share some technical clarity about it. And for the second approach (ayncFunc_newer) any suggestion on how it can be done better? var _retryCount = 0; //

how to properly throw an error if promise is rejected? (UnhandledPromiseRejectionWarning)

ε祈祈猫儿з 提交于 2019-11-28 10:10:40
问题 I have a promise and I would like an exception to be thrown if the promise is rejected. I tried this: var p = new Promise( (resolve, reject) => { reject ("Error!"); } ); p.then(value => {console.log(value);}); but I get a DeprecationWarning: (node:44056) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 2): Error! (node:44056) DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the

how to use es6-promises with typescript?

守給你的承諾、 提交于 2019-11-28 09:34:59
I read this SO question but having trouble getting promises to work with typescript. Hopefully we can make a clear guide. This is for a server/node project. I'm actually using latest iojs, but targeting ES5 as output. $ tsd query es6-promise --action install --save $ npm install --save es6-promise // typescript code: /// <reference path="../../typings/es6-promise/es6-promise.d.ts"/> var Promise = require("es6-promise").Promise; require('es6-promise').polyfill(); function test():Promise { var p:Promise = new Promise(); return p; } this is giving the error: Cannot find name 'Promise'. //

Retry a promise step

爱⌒轻易说出口 提交于 2019-11-28 08:57:21
问题 Suppose I have the the following Promise chain: var result = Promise.resolve(filename) .then(unpackDataFromFile) .then(transformData) .then(compileDara) .then(writeData); Now I have not only one transformData function but two or more, stored in an array. I want to try the first one, and if the compileData function fails, try the second one and so on until either compileData succeeds or the array of transformData functions is exhausted. Can someone give me an example on how to implement this?