Reputation: 1601
This code is working fine , but I need to add something in timeout function before return in firstFunction()
function LastFunction(){
var answer = firstFunction();
console.log(answer)
}
function firstFunction() {
//Do something;
return somevalue
}
How to add timeout function inside function before return value ?
function firstFunction() {
setTimeout (function(){
var something = "something here"
},1000)
return something
}
Upvotes: 1
Views: 513
Reputation: 1497
You'll need to use Promises. Your code inside the setTimeout
will be delayed but everything outside of it will continue to run synchronously.
function LastFunction(){
firstFunction().then(function(val) {
console.log(val)
return val
})
}
function firstFunction() {
var promise = new Promise(function(resolve, reject) {
setTimeout(function(){
var something = "something here"
resolve(something)
},1000)
})
return promise
}
Upvotes: 3