I am really hoping someone can help me with this issue despite my inability (confidentiality issues) to share too much information. Basically, I wrote this function to retry failed promises:
function retryOnFailure(func) {
return func().then(function(resp) {
return resp
}, function(err) {
return setTimeout(function() {
return retryOnFailure(func)
}, 5000)
}
)}
And then I have this code to utilize it:
function getStuff() {
return $.get('/some/endpoint')
}
retryOnFailure(getStuff).then(function(res) { console.log(res) })
Now the recursive retry function works fine, but my then in the last line of code doesn't fire in the event that an error case is reached in retryOnFailure. I know that the reason is because each recursive call creates a new promise so the promise that I'm listening to in my last line is not the same one that gets resolved, but I'm having difficulty figuring out how to remedy this. I want this function to be a generic retry function and as such don't want to handle the actual success in the method, but I'm not seeing any other way. Any help would be greatly appreciated!
return setTimeout(function() {
return retryOnFailure(func)
}, 5000)
looks like you're trying to do the right thing - return the next result from your .catch callback. However, setTimeout does not return a promise, so retryOnFailure() will be resolved immediately (with the timeout cancellation id).
Instead, you need to make sure that for all asynchronous functions that you use a promise is produced, so let's start by promisifying setTimeout:
function delay(ms) {
return new Promise(function(resolve, reject) {
setTimeout(resolve, ms);
});
}
Now we can use that to chain the retry after a delay, and get a promise from it:
function retryOnFailure(func) {
return func().then(null, function(err) {
return delay(5000).then(function() {
return retryOnFailure(func);
});
});
}
Notice that instead of .then(null, …) you can also use .catch(…).
This is the first way that came to mind, there is probably a more elegant solution:
function retryOnFailure(func, onSuccess) {
return func().then(function(resp) {
onSuccess(resp);
}, function(err) {
return setTimeout(function() {
return retryOnFailure(func, onSuccess)
}, 5000)
}
)}
retryOnFailure(getStuff, function(res) { console.log(res) });
The problem is your call to setTimeout, it breaks the flow of your promise. Try something like this instead:
function retryOnFailure(func) {
return new Promise(function(resolve) {
func().then(function(resp) {
resolve(resp);
}).catch(function(err) {
setTimeout(function() {
retryOnFailure(func).then(resolve);
}, 5000)
});
});
};
retryOnFailure(getStuff).then(function(res) { console.log(res) });
Note how I've created a new promise inside the function which never throws an error, but instead will simply attempt to process the func() and recurse itself on failure.
It also may be a good idea to put some kind of retry-limit as well.
Good luck!
Related
With promises, is it bad practice to leave out the use of reject? If I just want to return a Promise to ensure this function is run in it's entirety and not block whatever follows this function.
function testFunc() {
return new Promise((resolve,reject) => {
// do stuff
resolve(true);
})
}
testfunc().then(() => { // additional stuff});
If there is a chance of the do stuff throwing an error, then you should make sure to use reject and call it in case there's an error, so that the caller can see that there was a problem and (hopefully) handle it appropriately. For example, you wouldn't want to do:
function testFunc() {
return new Promise((resolve,reject) => {
getApi((error, result) => {
resolve(result);
});
});
}
but instead
function testFunc() {
return new Promise((resolve,reject) => {
getApi((error, result) => {
if (error) reject(error);
resolve(result);
});
});
}
If there's really no chance of the contents of the function erroring (barring logic bugs that could be unknowingly present in any code), then reject won't help you, because you don't really have any place suitable to put it. For example:
function someWeirdPromiseFunctionThatBlocks() {
return new Promise((resolve) => {
for (let i = 0; i < 1e5; i++) {
}
resolve();
});
}
Note that if you just want to:
not block whatever follows this function.
then just returning a Promise won't accomplish that, because the Promise constructor will run all of its code synchronously until it encounters something asynchronous (like getApi). To avoid blocking the running of sync code below, use Promise.resolve or setTimeout, eg:
function testFunc() {
return Promise.resolve()
.then(() => {
// do other stuff
});
No, its not necessary at all. reject is meant to be used in a similar way as one might use throw - it's for errors that happen.
In fact, if you're familiar with javascript's async/await syntax, this async function...
async function doSomething() {
throw new Error('Oh no!')
}
...roughly translates to this:
function doSomething() {
return Promise.reject(new Error('Oh no!'))
}
which shows how reject is intended to be used wherever you might normally throw an error.
Here's an example promise that I use often where there isn't any "reject" that would make sense:
const wait = ms => new Promise(resolve => setTimeout(resolve, ms))
wait(1000).then(() => console.log('done'))
Many others such examples exist.
I have a function called "test_sheet" that is supposed to return a value. that value will then be passed to a tester function which will tell me if I passed or failed the test.
inside my "test_sheet" I have a few async operations which are handled by promises.
now, how can I return a (non-promise) value from my test_sheet function.
function test_sheet()
{
//all my logic will go here
new Promise(function(resolve, reject)
{
//simulating an async operation
setTimeout(() => resolve(true), 1000);
})
.then(function(res){return res});
}
function tester()
{
//not allowed to change this function in any way
if(test_sheet() == true)
console.log("pass!");
else
console.log("fail!");
}
tester();
Is there any better way of doing this?
Well, technically it is possible, tester() may reamain intact:
var test_sheet=false;
function start_test()
{
//all my logic will go here
new Promise(function(resolve, reject)
{
//simulating an async operation
setTimeout(() => resolve(true), 1000);
})
.then(res => {
test_sheet=true;
tester();
});
}
function tester()
{
//not allowed to change this function in any way
test_sheet == true ? console.log("pass!") : console.log("fail!");
}
//tester();
start_test();
But the test starts with start_test() now, and test_sheet became a variable, with the sole purpose of acting as an argument - which could not be added to testing() without modifying it.
A nonworking bad design is transformed to working bad desing this way.
test_sheet() always returns a promise so try to get it resolved using async await or .then which feeds into the tester() function.
call you function this way:
test_sheet().then(function(test_sheet){
tester(test_sheet)})
for this you need to pass the boolean return value from test_sheet() to tester(test_sheet)
If you handle asynchronous code you have to use promise or callback and handle with async/await to change them to synchronous code
For example
function test_sheet()
{
//all my logic will go here
return new Promise(function(resolve, reject) {
//simulating an async operation
setTimeout(() => resolve(true), 2000);
})
}
async function tester()
{
//not allowed to change this function in any way
await test_sheet() == true ? console.log("pass!") : console.log("fail!");
}
tester();
I'm trying to implement a timeout for an asynchronous function using a promise and setTimeout. I want to run an asynchronous operation, and if it doesn't complete by a certain amount of time, throw an error. Currently, this is what I have: (some of it found on stackoverflow)
function someOperation(cb) {
setTimeout(function() {
cb(null, 'Done');
}, 2000);
}
var p = new Promise(function(resolve, reject) {
setTimeout(function() {
reject(new Error('Operation timed out'));
}, 100);
someOperation(function(err, res) {
if (err) reject(err);
else resolve(res);
});
});
p.then(function(res) {
console.log(res);
}, function(err) {
console.log("Throwing error...")
throw err;
});
But the program doesn't stop when the error is thrown. Would anybody be able to tell me why, and if there is an easier way to do this? I'd be very much appreciative.
EDIT: Now trying to use bluebird for the first time and it is giving someOperationPromised.timeout is not a function. Am I doing this correctly?
var Promise = require("bluebird");
function someOperation(cb) {
setTimeout(function() {
cb('', 'Done');
}, 2000);
}
var someOperationPromised = Promise.promisify(someOperation);
someOperationPromised.timeout(1).then(function (){
console.log("Finished!");
}).catch(Promise.TimeoutError, function (err) {
throw err;
});
Not sure what promise library you're using, if any. May I suggest Bluebird? It's faster than native and has a lot of great features. Among them? Timeout.
From the documentation:
var Promise = require("bluebird");
var fs = Promise.promisifyAll(require('fs'));
fs.readFileAsync("huge-file.txt").timeout(100).then(function(fileContents) {
}).catch(Promise.TimeoutError, function(e) {
console.log("could not read file within 100ms");
});
Edit:
Hey there! I took some time sorting through this, trying to figure out why your edit didn't work. My goal is to prove to you that .timeout() works. Moreover, I would like to provide you with a workable solution you can use moving forward with Bluebird.
Below I include a function I've named takesFourSeconds. It returns a promise that will resolve after 4 seconds. I then call takesFourSeconds twice. The first time I use timeout to force the promise chain to reject if it takes more than 3 seconds. The second time I force it to reject if it takes longer than 5 seconds.
var Promise = require("bluebird");
function takesFourSeconds (){
return new Promise((resolve, reject) => {
setTimeout(function(){
return resolve('hi');
}, 4000);
});
}
takesFourSeconds().timeout(3000).then(function(res) {
console.log(res);
}).catch(Promise.TimeoutError, function(e) {
console.log("promise took longer than 3 seconds");
});
takesFourSeconds().timeout(5000).then(function(res) {
console.log(res);
}).catch(Promise.TimeoutError, function(e) {
console.log("promise took longer than 5 seconds");
});
Note that this returns:
$ node index.js
promise took longer than 3 seconds
hi
As expected.
Small side note:
When creating a function that returns a promise, you don't have to create a new promise for every function you call in a chain of promises. Only the first function.
For example, if I wanted to call another function after takesFourSeconds() I could write it like this:
function myFunc(result){
return result === 'hi' ? resolve('hey there') : reject('hi there');
}
then:
takesFourSeconds()
.then(myFunc) // myFunc will take in the resolved value of takesFourSeconds implicitly
.then(result => console.log(result)
.catch(error => console.log(error);
This should output:
"hey there"
There you have it! Timeouts in Bluebird.js. :)
I use the following code and there is something that a bit confuse me
if I put in the timeout 1000ms I see that the promise called in the right order
but if I change it to the following
This is step 3
This is step 2
This is step 1
I guess that this happen since when the function is resolved then he proceed to the next function am I correct ? if this is true how can I do this chain that when the first is finished then he proceed to the second etc but without using the promise hell :-)
https://jsfiddle.net/2yym400j/
var step1 = function(ms) {
return new Promise(function(resolve, reject) {
setTimeout(function() {
console.log("This is step 1");
resolve();
}, ms);
})
}
var step2 = function(ms) {
return new Promise(function(resolve, reject) {
setTimeout(function() {
console.log("This is step 2");
resolve();
}, ms);
})
};
var step3 = function(ms) {
return new Promise(function(resolve, reject) {
setTimeout(function() {
console.log("This is step 3");
resolve();
}, ms);
})
};
step1(500)
.then(step2(300))
.then(step3(200))
.catch(function(err) {
console.log(err);
});
Just pass a function instead of the result of the steps.
step1(500)
.then(function() { return step2(300); })
.then(function() { return step3(200); })
.catch(function(err) {
console.log(err);
});
Without this, you are just calling each step without "blocking" for the previous step to resolve.
I know you've already gotten an answer as to why your original code didn't work, but I thought I'd show another way to approach it that is a bit more DRY than what you were doing. You can create a single function that returns a function and then you can use that in the fashion you were using because it returns a function that will be called later which is what .then() wants. So, you can do something like this:
function delay(t, msg) {
return function() {
return new Promise(function(resolve) {
setTimeout(function() {
console.log(msg);
resolve();
}, t);
});
}
}
delay(500, "This is step 1")()
.then(delay(300,"This is step 2"))
.then(delay(200, "This is step 3"))
.catch(function(err) {
console.log(err);
});
Working demo here: https://jsfiddle.net/jfriend00/mbpq4g8m/
Daniel White's comment and answer are correct, but I thought an additional explanation might help.
Your original code broke two rules:
then can't take a Promise, but it can take a function that returns a Promise. If you pass in anything except a function, it will be ignored, just like you passed in null instead.
As soon as you call new Promise(someFunction), the someFunction executes asynchronously without waiting for anything. This means you can call new Promise from within a then function, but if you call it early then it won't have the delayed behavior you're expecting.
So given that each call to stepN returns a newly-constructed promise, I've inlined everything that happens synchronously and renamed the results constructAndReturnPromiseN and constructedPromiseThatStepNReturns. That makes your original code looks like this so all the promises happen at once:
constructedPromise1ThatStep1Returns
.then(constructedPromise2ThatStep2Returns) // BAD: Don't pass promises into then.
.then(constructedPromise3ThatStep3Returns) // BAD: Don't pass promises into then.
.catch(function(err) {
console.log(err);
});
...where Daniel White's code does this:
constructedPromise1ThatStep1Returns
// GOOD: The then function returns a promise that is *constructed inside* the function.
.then(function() { return constructAndReturnPromise2(300); })
.then(function() { return constructAndReturnPromise3(200); })
.catch(function(err) {
console.log(err);
});
In the example below, would it be possible for the server response to doSomethingOnes request to come back before the server response to doSomethingTwo's request?
$scope.pace = $interval (
function() {
// code
}, 1000, 10
).then(postResult)
.then(doSomethingOne)
.then(doSomethingTwo);
Question being because, doSomethingOne is posting data to the db, and then doSomethingTwo is querying the db and returning some data, including data that doSomethingOne should have posted.
But the response to doSomethingTwo's request doesn't include the most recent data posted by doSomethingOne (until the $scope.pace is run again from the beginning).
I don't have a strong understanding of callbacks (despite a lot of reading about them) so any advice would be very appreciated.
Brief Clarification
doSomthingOne does an $http.post() and doSomethingTwo does an $http.get(). No promises are used here.
Update:
From your edit:
doSomthingOne does an $http.post() and doSomethingTwo does an $http.get(). No promises are used here.
Well, $http.post returns a promise (sometimes called a "future" in the documentation), but if you're not using it, then nothing prevents doSomethingTwo from being called before the POST is completed. In fact, it's very likely to be called before the POST completes (long before).
You can probably fix the problem simply by returning the promise $http.post returns (or returning the promise created by your call on that promise, if you're using one). E.g.:
function doSomethingOne() {
return $http.post(/*...args...*/);
}
or
function doSomethingOne() {
return $http.post(/*...args...*/).then(/*...*/);
}
Details in the below.
Original Answer (still relevant):
It depends on what doSomethingOne does and what it returns. If doSomethingOne starts an asynchronous process but doesn't return a promise for that process, then doSomethingTwo can be called before that process is complete. If doSomethingOne does its work synchronously (unlikely given what you've said) or returns a promise for its asynchronous work, it will complete before doSomethingTwo is called because doSomethingTwo waits for that promise to be settled.
Here's an example where doSomethingOne doesn't return a promise for its async work, and so doSomethingTwo is likely to run before doSomethingOne's async work completes:
// Simulate an asynchronous DB call
function dbCall(data) {
return new Promise(function(resolve) {
setTimeout(function() {
resolve("Result for " + data);
}, Math.floor(Math.random() * 500));
})
}
function start() {
console.log("start called");
return new Promise(resolve => {
setTimeout(() => {
console.log("start resolving");
resolve();
}, 0);
})
}
function doSomethingOne() {
// THIS IS PROBABLY WRONG
console.log("doSomethingOne called");
dbCall("one data").then(function(result) {
console.log("doSometingOne's async is done");
});
}
function doSomethingTwo() {
console.log("doSomethingTwo called");
}
start().then(doSomethingOne).then(doSomethingTwo);
Live copy on Babel's REPL
That's probably wrong. Instead, you want doSomethingOne to return a promise from its async work; it can do that by just returning the result of its call on dbCall(...).then:
// Simulate an asynchronous DB call
function dbCall(data) {
return new Promise(function(resolve) {
setTimeout(function() {
resolve("Result for " + data);
}, Math.floor(Math.random() * 500));
})
}
function start() {
console.log("start called");
return new Promise(resolve => {
setTimeout(() => {
console.log("start resolving");
resolve();
}, 0);
})
}
function doSomethingOne() {
console.log("doSomethingOne called");
return dbCall("one data").then(function(result) {
//^^^^^^^------------------------------------------- change is here
console.log("doSometingOne's async is done");
});
}
function doSomethingTwo() {
console.log("doSomethingTwo called");
}
start().then(doSomethingOne).then(doSomethingTwo);
Live copy on Babel's REPL