es6-promise

gulp run sequence after promise resolve

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-11 06:33:33
问题 Main task is provided to user availability to put login and password before each run task. So i try to use promises, but in such case my sequence doesn`t start because task finished when promise resolved. export default function() { var loginPromise = new Promise((resolve, reject) => { inquirer.prompt(config.loginQuestions).then(answer => { if (!(answer.login.length && answer.password.length)) { reject('loginError') } else { resolve(answer) } }); }); return loginPromise; }; And task import

Javascript Exceptions are hidden in Promises. How can I display them without catch?

与世无争的帅哥 提交于 2019-12-11 06:16:17
问题 Edit: Promises do not hide exceptions, as was pointed out by the excellent (accepted) answer. I want to elaborate on how exceptions in promises were invisible: I was working on a project that used a propriatary library to make ajax calls. Now in case something in the callback of the ajax-call went wrong an exception would be thrown. This exception would then trigger the reject() to be called, which was then silently discarded. In fact the exceptions were not hidden, but actively disposed of

Wait for promises inside Promise.all to finish before resolving it

拟墨画扇 提交于 2019-12-11 05:05:33
问题 I have a Promise.all that executes asynchronous functions mapped on an array input if it's not null and then resolve data to a previously defined Promise: Promise.all((inputs || []).map(input => { return new Promise((resolve, reject) => { someAsyncFunc(input) .then(intermediateOutput => { someOtherAsyncFunc(intermediateOutput ) .then(output => { return Promise.resolve(output ) }) .catch(reason=> { return Promise.reject(reason) }) }) .catch(reason => { return Promise.reject(reason); }) })

How to use multiple XMLHttpRequest.responseText values?

眉间皱痕 提交于 2019-12-11 04:52:59
问题 In javascript, how can I best combine multiple values which are obtained as arguments to callback functions only, preferably without adding library dependencies? E.g. consider function eventHandler() { getSomethingAsync(function(something){ getOtherAsync(function(other){ console.log([something.value, other.status]); }); }); } This looks like the starting point of CallbackHell(.com). In some other languages I would use promises at this point, along the lines of function eventHandler() { var

How to use Promise.all() when I'd like to pass an array variable as an argument

谁说胖子不能爱 提交于 2019-12-11 04:45:19
问题 I'm in a problem using Promise.all(). I'd like to pass an array variable as an argument to Promise.all() like below. const promArr = [] if (condition1) { promArr.push(() => prom1(arg1, arg2)) } if (condition2) { promArr.push(() => prom2(arg1, arg2)) } if (promArr.length > 0) Promise.all(promArr) But above doesn't run the promise functions( prom1 , prom2 ) even if conditions are all true. ( promArr.length is as I expected) if I push promise functions directly to promArr , I'm afraid they run

Retrieve data from a callback thats in a promise?

两盒软妹~` 提交于 2019-12-11 04:29:54
问题 I have the following piece of code right now: const Promise = require('bluebird'); const readFile = Promise.promisify(fs.readFile); recordPerfMetrics: function(url) { var self = this; var perf, loadTime, domInteractive, firstPaint; var perfData = {}; readFile('urls.txt', 'UTF-8').then(function (urls, err) { if (err) { return console.log(err); } var urls = urls.split("\n"); urls.shift(); urls.forEach(function(url) { console.log(url); self.getStats(url).then(function(data) { data = data[0];

Javascript Promises : Can I know which part of promise chain caused error?

南楼画角 提交于 2019-12-11 04:29:22
问题 (Please excuse my English) I am learning about javascript promises, now. Below sample code is a simple javascript code for node.js( my node.js version is v10.0.0 ), which asynchronously reads and parses a JSON file using promise chain. const fs = require("fs"); function readFileAsync(filename) { return new Promise((resolve, reject) => { fs.readFile(filename, 'utf8', (error, result) => { if (error) reject(error); else resolve(result); }); }); } readFileAsync('test.json') .then(res => JSON

How to return a Promise from async function?

社会主义新天地 提交于 2019-12-11 03:26:10
问题 When I try to return a promise from an async function, it's impossible to distinguish the status of the returned Promise and the function. I think, the simplest solution is to put the promise to be returned in an array. The following is a stupid example, but I hope it demonstrates the problem: function loadMetaData(id) {/*...*/} // Returns Promise<MetaData> function loadSingleData(name) {/*...*/} // Returns Promise<SingleData> async function startLoadingSingleData(id, object) { const metaData

Get value of resolved Promise in sync

眉间皱痕 提交于 2019-12-11 02:45:00
问题 If we know that a Promise is definitely resolved, how can we access the value and if we can't, why not? let a = Promise.resolve(123); console.log(a.value); // ??? The following does not work- it prints "First, Last, 123" console.log("First"); Promise.resolve(123).then(console.log); console.log("Last"); I'm asking how to get the value of an already resolved Promise synchronously and if that's not possible, why not? 回答1: No, it is not possible to do this. This is by design. The Promise A+

Return after all promises resolved [duplicate]

為{幸葍}努か 提交于 2019-12-11 02:37:40
问题 This question already has answers here : How do I return the response from an asynchronous call? (36 answers) Closed 3 years ago . Having a code sample below, I'd like to get baz variable returned from 'main' function after all promises resolved. exports.foo = function(bar) { var baz; // some kind of promises are here forming array of promises p // some of promises may change the baz variable Promise.all(p).then(() => { // returning expression for main function is here // return baz here //