es6-promise

ES6 promise settled callback?

大兔子大兔子 提交于 2020-01-06 16:22:33
问题 I want to run the same action whether my Promise resolved successfully or not. I don't want to bind the same function to both args of .then . Isn't there a .always like jQuery has? If not, how do I achieve this? 回答1: Isn't there a .always like jQuery has? No, there's not (yet). Though there is an active proposal, so maybe ES2018. If not, how do I achieve this? You can implement the finally method yourself like this: Promise.prototype.finally = function(cb) { const res = () => this const fin =

async/await return Promise { <pending> }

泪湿孤枕 提交于 2020-01-06 13:10:43
问题 my question is: why does this log 'promise {pending}' despite of i used async/await? I checked similar questions and answers on them and it seems like it should be okay but it is not. How do i change it to get the result and why? Thank you. const rp = require('request-promise-native'); async function sampleFunc() { let sample = await rp({ uri: 'http://google.com', jar: true }); return sample; } async function f() { return await sampleFunc(); } console.log( f()); 回答1: async-await is really

Wait until openlayers 5 geolocation returned a value

北慕城南 提交于 2020-01-05 05:26:26
问题 I am using openlayers 5 to implement a simple geolocation functionality : When check the geolocation checkbox, the geolocation starts, zooms to the position with an animated transition and it automatically tracks the position constantly. It adds a feature in a vector layer. When uncheck the geolocation checkbox, the geolocation is turned off, along with the tracking and that vector layer is cleared. My problem is that I don't know how to wait until the geolocation has a value , so I can

React : Search Filter is not working Properly

余生颓废 提交于 2020-01-04 14:20:52
问题 I am fetching records from server through API , API was built in Loopback . Actually On every page I am showing 5 records , currently It working fine I can navigate next or prev through pagination button and on every page it showing 5 records . Problem is that when User type something in search box , record are fetching correctly but when user remove query from search box it break the application flow . I mean to say that It showing all data not like 5 . I want that when user search something

Why can't I use Promise.resolve with an osmosis instance?

柔情痞子 提交于 2020-01-04 01:52:50
问题 I am trying to understand why these console.log statements behave differently. I expect them to behave the same: Using Node 7. Consider the following cases: 1. Promise.resolve(object) Promise.resolve handles objects as I'd expect: Promise.resolve({ a: `hello` }).then(console.log) // { a:'hello' } 2. Directly console.log a class instance from a library. If I store an Osmosis instance I can console.log it: const osmosis = require(`osmosis`) console.log(new osmosis.get(url)) /* { prev: {

async/await clarity, with sleep example

╄→尐↘猪︶ㄣ 提交于 2020-01-04 01:44:16
问题 I am trying to get hang of async/await with below implementation but it is not working as expected public static async sleep(ms: number): Promise<void> { await Utilities._sleep(ms); } private static _sleep(ms: number): Promise<{}> { return new Promise((resolve: Function) => setTimeout(resolve, ms)); } _sleep will resolve promise after n milliseconds, and await should sleep till that time.. but below test of mine is failing it("should sleep for 500 ms", ()=> { const date1 = (new Date())

Recursive Tree Walk with ES6 Promise

核能气质少年 提交于 2020-01-03 04:46:25
问题 I'm looking to walk an object tree of unknown depth and return a given node via an ES6 promise. (used lodash here, obviously not necessary, I realize). I've got the tree walking working fine but I'm a bit unclear the proper method to ensure that the top-level scope variable promise is passed into the recursive function calls so that it's available when calling .resolve( data ) . Right now it attempts to execute on a successful find but fails to resolve the promise since the recursive function

Stop other promises when Promise.all() rejects

别来无恙 提交于 2020-01-02 06:49:26
问题 While all the questions about Promise.all focus on how to wait for all promises , I want to go the other way -- when any of the promises fails, stop the others, or even stop the whole script. Here's a short example to illustrate: const promise1 = new Promise((resolve, reject) => { setTimeout(resolve, 1000, 'resolve1'); }).then(a => { console.log('then1'); return a; }); const promise2 = new Promise((resolve, reject) => { setTimeout(reject, 2000, 'reject2'); }).then(a => { console.log('then2');

ES6 Promise patterns for exotic control flows

久未见 提交于 2020-01-01 12:10:05
问题 ES6 Promises are great. So far it’s been pretty easy to adjust my thinking from the callback idiom. I’ve found it naturally encourages more modular code, and of course error handling is much clearer. But a few times I’ve encountered flow situations that don’t seem(?) like they can be readily translated from nodebacks to promises (and perhaps that’s just that, but maybe I’m just blind to the answers). Since promises are agnostic about the next operation (or if there even is one), it seems

ES6 Promise patterns for exotic control flows

风流意气都作罢 提交于 2020-01-01 12:09:39
问题 ES6 Promises are great. So far it’s been pretty easy to adjust my thinking from the callback idiom. I’ve found it naturally encourages more modular code, and of course error handling is much clearer. But a few times I’ve encountered flow situations that don’t seem(?) like they can be readily translated from nodebacks to promises (and perhaps that’s just that, but maybe I’m just blind to the answers). Since promises are agnostic about the next operation (or if there even is one), it seems