promise

Async function returning promise, instead of value

ぃ、小莉子 提交于 2020-07-15 05:10:26
问题 I'm trying to understand how async/await works in conjunction together with promises. Code async function latestTime() { const bl = await web3.eth.getBlock('latest'); console.log(bl.timestamp); // Returns a primitive console.log(typeof bl.timestamp.then == 'function'); //Returns false - not a promise return bl.timestamp; } const time = latestTime(); // Promise { <pending> } Issue As far as I understand, await should be blocking and in the code above it seemingly blocks returning an object bl

Can you write this without using a Deferred?

谁说胖子不能爱 提交于 2020-07-14 06:48:31
问题 I wrote some code below that uses promises and the easiest way I could find to write it was using a Deferred object instead of the usual Promise executor function because I need to resolve the promise from outside the executor. I'm wondering if there's an accepted design pattern based on the Promise executor function for a problem like this that doesn't use a deferred-like solution? Can it be done without having to resolve the promise from outside the promise executor? Here are the details. I

How do I unsubscribe from a promise in bluebird which is using reacthooks?

帅比萌擦擦* 提交于 2020-07-10 10:27:43
问题 This error has been discussed before in many blogs,forums..etc and I'm speculating that the problem is with not having a promise that is cancellable.I'm wondering how to unsubscribe from a a bluebird promise. useEffect(() => { let isFetchable = !!qualifiedURL; const fetchData = async () => { let fetcher = Promise.resolve(); if (isFetchable && !data) { setLoading(true); if (method === 'GET') { fetcher = fetch(method, qualifiedURL); } else { fetcher = fetch(method, qualifiedURL, { ...options, }

How do I unsubscribe from a promise in bluebird which is using reacthooks?

情到浓时终转凉″ 提交于 2020-07-10 10:26:28
问题 This error has been discussed before in many blogs,forums..etc and I'm speculating that the problem is with not having a promise that is cancellable.I'm wondering how to unsubscribe from a a bluebird promise. useEffect(() => { let isFetchable = !!qualifiedURL; const fetchData = async () => { let fetcher = Promise.resolve(); if (isFetchable && !data) { setLoading(true); if (method === 'GET') { fetcher = fetch(method, qualifiedURL); } else { fetcher = fetch(method, qualifiedURL, { ...options, }

Execute batch of promise with Promise.allSettled()

北城余情 提交于 2020-07-06 12:58:48
问题 With node v10.15.1 I try to use Promise.allSettled() to executes batches of Promise but it throw me an error TypeError: Promise.allSettled is not a function Is Promise.all() returning a promise ? The Main function below return an object. Other functions use some Promises to create its "sub-object". Why I need "batch of promise" : To create a sub-object, all the required promises must be settled. But not all the sub-objects are needed in the "main object". const path = require('path'); const

Promises in redux-saga

早过忘川 提交于 2020-07-05 07:02:51
问题 I found the same question here, but without a proper answer I am looking for. I am developing a simple application with CRUD operations. On the edit page, after the component gets mounted ( componentDidMount() ), the app dispatches an action to retrieve a specific post details: dispatch({ type: FETCH_POST, id: 'post-id' }) I am using redux-saga and want the above call to return a Promise so that I can access the API response. Right now, without a callback/Promise, I ended up with defining a

Promises, how to pass variable into .then function

天涯浪子 提交于 2020-07-05 06:56:57
问题 Hello this is a question to help me understand how Promises .then returns work. The question is: how can I scoped variables to the second .then chained function? Here is a jsbin http://jsbin.com/xacuna/edit?js,output I can access the global variables, and pass in the scoped variables to the first then, but not after. let innerReturnFunction = (res, myName) => { /* this works */ console.log(`hi from inner name: ${myName}`) return res } let getInnerFuncVariable = () => { var myName = 'arturo'

Axios handling errors

对着背影说爱祢 提交于 2020-07-04 06:42:46
问题 I'm trying to understand javascript promises better with Axios. What I pretend is to handle all errors in Request.js and only call the request function from anywhere without having to use catch() . In this example, the response to the request will be 400 with an error message in JSON. This is the error I'm getting: Uncaught (in promise) Error: Request failed with status code 400 The only solution I find is to add .catch(() => {}) in Somewhere.js but I'm trying to avoid having to do that. Is

Async function returns [object Promise]

廉价感情. 提交于 2020-07-04 02:47:46
问题 I'm trying to return an async function but I either get promise: < { PENDING } > or [object Promise] instead of [object Object] I've tried returning the value using Promise.resolve(value) , Promise.resolve().then(return value) , return new Promise((resolve, reject) => {resolve(value)} from top-level to bottom my code looks like: //Top-Level const getNext = require('getNext'); const next = getNext({ page, value, name, id, }); //getNext const controllerMap = { intro: introController }; async

Async function returns [object Promise]

耗尽温柔 提交于 2020-07-04 02:46:57
问题 I'm trying to return an async function but I either get promise: < { PENDING } > or [object Promise] instead of [object Object] I've tried returning the value using Promise.resolve(value) , Promise.resolve().then(return value) , return new Promise((resolve, reject) => {resolve(value)} from top-level to bottom my code looks like: //Top-Level const getNext = require('getNext'); const next = getNext({ page, value, name, id, }); //getNext const controllerMap = { intro: introController }; async