I need such sequence:
🤡lurks in the shadows;
the end
How to achieve such result without touching 'sequential' function?
Achieve sequential run
function who() {
return new Promise(resolve => {
setTimeout(() => {
resolve('🤡');
}, 200);
});
}
function what() {
return new Promise(resolve => {
setTimeout(() => {
resolve('lurks');
}, 300);
});
}
function where() {
return new Promise(resolve => {
setTimeout(() => {
resolve('in the shadows');
}, 500);
});
}
async function msg() {
const a = await who();
const b = await what();
const c = await where();
console.log(`${ a } ${ b } ${ c }`);
}
function sequential(){
msg();
console.log('the end');
}
sequential();
I need such sequence:
🤡lurks in the shadows
the end
How to achieve such result without touching 'sequential' function?
Current result is:
the end
🤡lurks in the shadows
function who(data) {
return new Promise(resolve => {
setTimeout(() => {
resolve(data + '🤡');
}, 200);
});
}
function what(data) {
return new Promise(resolve => {
setTimeout(() => {
resolve(data + 'lurks');
}, 300);
});
}
function where(data) {
return new Promise(resolve => {
setTimeout(() => {
resolve(data + 'in the shadows');
}, 500);
});
}
function msg() {
return who('')
.then(what)
.then(where);
}
function sequential(){
return msg()
.then((respo) => {
console.log(JSON.stringify(respo));
console.log('the end');
});
}
sequential();
You can do this!
Related
I had a job interview yesterday, and I was given a coding challenge as the following:
// 3. Coding challenge.
// The goal is to make function1/function2 to work only when the constructor has finished its async operations.
// You CAN'T change the notifyUrls function. Imagine it's a 3th party library you don't have control on.
// CAN'T CHANGE THIS.
//===================
function notifyUrls(item, callback) {
asyncOperation(item).then((res) => {
callback(res);
});
}
//===================
const asyncOperation = () => { return new Promise((resolve, reject) => { setTimeout(() => { console.log('Timeout execute'); resolve(); }, 2000); }); };
const URL1 = 'http://www.somerestapi/get1';
const URL2 = 'http://www.somerestapi/get2';
const URL3 = 'http://www.somerestapi/get3';
class MyClass {
constructor() {
[URL1, URL2, URL3].forEach(item => {
notifyUrls(item, () => { });
});
}
myFunction1() {
// Only start working when constructor finished notifying.
// ...
console.log('myFunction1');
}
myFunction2() {
// Only start working when constructor finished notifying.
// ...
console.log('myFunction2');
}
}
And here is what I did:
// CAN'T CHANGE THIS.
//===================
function notifyUrls(item, callback) {
asyncOperation(item).then((res) => {
callback(res);
});
}
//===================
const asyncOperation = () => { return new Promise((resolve, reject) => { setTimeout(() => { console.log('Timeout execute'); resolve(); }, 2000); }); };
const URL1 = 'http://www.somerestapi/get1';
const URL2 = 'http://www.somerestapi/get2';
const URL3 = 'http://www.somerestapi/get3';
class MyClass {
constructor() {
this.ready = Promise.all([URL1, URL2, URL3].map((url) => { this.myAsyncCall(url); }));
}
myAsyncCall(item) {
return new Promise((resolve, reject) => {
notifyUrls(item, (res) => { resolve(res); });
});
}
async myFunction1() {
if (await this.ready) {
// Only start working when constructor finished notifying.
// ...
console.log('myFunction1');
}
}
myFunction2() {
// Only start working when constructor finished notifying.
// ...
console.log('myFunction2');
}
}
(async () => {
const myClass = new MyClass();
await myClass.myFunction1();
})();
But the output is:
myFunction1
Timeout execute
Timeout execute
Timeout execute
What I want the output to be is:
Timeout execute
Timeout execute
Timeout execute
myFunction1
How can I work around it?
Thanks.
The problem is in
this.ready = Promise.all([URL1, URL2, URL3].map((url) => { this.myAsyncCall(url); }));
Your map callback doesn't return anything
Either do
this.ready = Promise.all([URL1, URL2, URL3].map((url) => { return this.myAsyncCall(url); }));
or
this.ready = Promise.all([URL1, URL2, URL3].map((url) => this.myAsyncCall(url)));
Adding some coding tips
notifyUrls(item, (res) => {
resolve(res);
});
Can simply be
notifyUrls(item, resolve);
and
const asyncOperation = () => {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve();
}, 2000);
});
};
is just
const asyncOperation = () => new Promise(resolve => setTimeout(resolve, 2000));
Hello I'm new to javascript and wondering if there is a way to covnert below setInterval thingy into a promise so that .then could be used instead of the callback. Any help?
My ideas:
With a setTimeout I could resolve after a fixed time. But I'm not getting any ideas dealing with setInterval...
function alert_above(scrip, price, callback) {
var intvl = setInterval(() => {
if (get_last_price(scrip) > price) {
callback();
clearInterval(intvl);
}
}, 1000);
return intvl;
}
You can create a promise function that resolves asynchronously. Read more about Promise Here
function myInterval() {
return new Promise(resolve => {
const intId = setInterval(() => {
clearInterval(intId);
resolve();
}, 1000)
})
}
myInterval().then(() => {
console.log('Called after 1 second');
})
I think you could wrap into a new Promise like :
function promisifySetInterval(time) {
var defer = new Promise(resolve => {
let counter = 0
var intvl = setInterval(() => {
if (counter > time) {
resolve('hey');
clearInterval(intvl);
} else {
counter += 1000
}
}, 1000);
})
return defer;
}
promisifySetInterval(2000).then(param => {
console.log('hey', param)
})
And for youre case something like this :
function alert_above(scrip, price) {
var defer = new Promise(resolve => {
var intvl = setInterval(() => {
if (get_last_price(scrip) > price) {
resolve('hey');
clearInterval(intvl);
}
}, 1000);
})
return defer;
}
alert_above().then(param => {
console.log('hey', param)
})
Note: poll will be executed without delay the first time, which is different from the native setInterval.
Q: Why is poll based on setTimeout not setInterval?
A: Please see Execute the setInterval function without delay the first time.
Implementation:
// Promisify setTimeout
const pause = (ms, cb, ...args) =>
new Promise((resolve, reject) => {
setTimeout(async () => {
try {
resolve(await cb?.(...args))
} catch (error) {
reject(error)
}
}, ms)
})
// Promisify setInterval
const poll = async (interval, times, cb, ...args) => {
let result
const resolve = value => (times = 0) || (result = value)
const reject = reason => (times = 0) || (result = Promise.reject(reason))
await (async function basePoll() {
if (times > 0) {
const _result = await cb(...args, resolve, reject)
if (times) {
result = _result
--times && (await pause(interval, basePoll))
}
}
})()
return result
}
Tests:
import ordinal from 'ordinal'
// Test 1
poll(1000, 3, (a, b, c) => [a, b, c], 1, 2, 3).then(value => console.log(value))
// Test 2
let times = 0
poll(1000, 5, resolve => {
console.log(`${ordinal(++times)} time`)
times === 3 && resolve('resolved')
}).then(value => console.log(value))
// Test 3
let times = 0
poll(1000, 5, (resolve, reject) => {
console.log(`${ordinal(++times)} time`)
times === 3 && reject('rejected')
}).catch(error => console.error(error))
I have some example code, but I can't figure out why the console is logging things out of order.
async function myAsyncFunction() {
setTimeout(() => { console.log("1st"); return "something"; }, 500);
}
async function mainline() {
let promise = new Promise((resolve, reject) => {
setTimeout(() => { console.log("0th"); resolve(); }, 500);
});
promise.then(async () => {
return await myAsyncFunction();
})
.then((string) => {
console.log(string);
console.log("2nd");
});
}
mainline();
The console logs:
> 0th
> undefined
> 2nd
> 1st
So clearly my mainline isn't waiting to resolve the async function. What did I do wrong?
Your myAsyncFunction isn't returning anything. You need to wrap it in a promise.
function myAsyncFunction() {
return new Promise( (res, rej) =>
setTimeout(() => { console.log("1st"); res("something"); }, 500) );
}
function myAsyncFunction() {
return new Promise((resolve, reject) => {
setTimeout(() => { console.log("1st"); resolve("something"); }, 500);
});
}
async function mainline() {
let promise = new Promise((resolve, reject) => {
setTimeout(() => { console.log("0th"); resolve(); }, 500);
});
promise.then(async () => {
return await myAsyncFunction();
})
.then((string) => {
console.log(string);
console.log("2nd");
});
}
mainline();
I have a javascript function with setTimeOut and I am retrying to call same function, if any error from API call.I am calling same function in catch block.Is my node server is going crash and resources will be blocked or it will keep calling getData() function
let retry = ()=> {
setTimeout(() => {
getData()
retry()
}, 3000);
}
let getData = () =>{
Someapi.getData().then((token) => {
console.log(`Data after 3 seconds->${token}`)
}).catch((err) => {
getData()
})
}
I do not know if this work.
let retry = () => {
setTimeout(() => {
getData();
retry();
}, 3000);
};
while (true) {
let getData = () => {
Someapi.getData()
.then(token => {
console.log(`Data after 3 seconds->${token}`);
return false;
})
.catch(err => {
return true;
});
};
}
I use this retry code in my project, it works well in production:
const pause = (duration) => {
return new Promise(resolve => setTimeout(resolve, duration));
};
const retry = (retryTimes, func, delay) => {
return func().catch(
(err) => {
if(retryTimes > 0) {
return pause(delay).then(
() => retry(retryTimes - 1, func, delay * 2)
);
} else {
return Promise.reject(err);
}
}
);
};
I am trying to test the following snippet using the Chrome Dev Tool, but I get this error on the last line of the snippet ... cannot find where it's wrong ...
function myPromise1(time1, ok1){
return new Promise((resolve, reject) => {
// do stuff 1 async
setTimeout(() => {
if ( ok1 ) {
resolve('stuff-1 worked')
} else {
reject(Error('stuff-1 failed'))
}
}, time1)
});
function myPromise2(time2, ok2){
return new Promise((resolve, reject) => {
// do stuff 2 async
setTimeout(() => {
if ( ok2 ) {
resolve('stuff-2 worked')
} else {
reject(Error('stuff-2 failed'))
}
}, time2)
});
function myPromise3(time3, ok3){
return new Promise((resolve, reject) => {
// do stuff 3 async
setTimeout(() => {
if ( ok3 ) {
resolve('stuff-3 worked')
} else {
reject(Error('stuff-3 failed'))
}
}, time3)
});
const c1 = '';
const c2 = '';
const c3 = '';
const promise1 = (val11, val12) => myPromise1(val11, val12)
.then(p1Result => {
c1 = p1Result;
return;
}, function(err1) {
console.log('Error: ', err1);
return;
});
const promise2 = (val21, val22) => myPromise2(val21, val22)
.then((p2Result) => {
c2 = p2Result;
return;
}, function(err2) {
console.log('Error: ', err2);
return;
});
const promise3 = (val31, val32) => myPromise2(val31, val32)
.then((p3Result) => {
c3 = p3Result;
return;
}, function(err3) {
console.log('Error: ', err3);
return;
});
const conditionalPromiseFlow = (...fns) => {
if(fns.length === 0) return Promise.resolve()
const [next] = fns;
return next()
.then(result => {
if(result) {
return conditional(...fns.slice(1));
}
return result;
})
}
conditionalPromiseFlow(() => promise1(1000, true), () => promise2(2000, true), () => promise3(3000, true))
.then(() => {
console.log('Status 200 - ALL DONE');
return;
})
.catch((error) => {
console.log('Status ', error.status, ' - ', error.message);
return;
}) <== Error stated here
this is the corrected snippets to test promises flow...
I can play with the main() line
conditionalPromiseFlow(() => promise1(1000, true), () => promise2(2000, true), () => promise3(3000, true))
the flow stops when one promise is rejected
function myPromise1(time1, ok1){
console.log('Promise1', time1, ok1);
return new Promise((resolve, reject) => {
setTimeout(() => {
if ( ok1 ) {
resolve('stuff-1 worked');
} else {
reject(Error('stuff-1 failed'));
}
}, time1);
});
}
function myPromise2(time2, ok2){
console.log('Promise2', time2, ok2);
return new Promise((resolve, reject) => {
setTimeout(() => {
if ( ok2 ) {
resolve('stuff-2 worked');
} else {
reject(Error('stuff-2 failed'));
}
}, time2);
});
}
function myPromise3(time3, ok3){
console.log('Promise3', time3, ok3);
return new Promise((resolve, reject) => {
setTimeout(() => {
if ( ok3 ) {
resolve('stuff-3 worked');
} else {
reject(Error('stuff-3 failed'));
}
}, time3);
});
}
let c1 = '';
let c2 = '';
let c3 = '';
const promise1 = (val11, val12) => myPromise1(val11, val12)
.then(p1Result => {
c1 = p1Result;
return;
}, function(err1) {
err1.status = 300;
throw err1;
});
const promise2 = (val21, val22) => myPromise2(val21, val22)
.then((p2Result) => {
c2 = p2Result;
return;
}, function(err2) {
err2.status = 300;
throw err2;
});
const promise3 = (val31, val32) => myPromise3(val31, val32)
.then((p3Result) => {
c3 = p3Result;
return;
}, function(err3) {
err3.status = 300;
throw err3;
});
const conditionalPromiseFlow = (...fns) => {
if(fns.length === 0) return Promise.resolve();
const [next] = fns;
return next()
.then(result => {
if(!result) {
return conditionalPromiseFlow(...fns.slice(1));
}
return result;
});
};
conditionalPromiseFlow(() => promise1(1000, true), () => promise2(2000, true), () => promise3(3000, true))
.then(() => {
console.log('Status 200 - ALL DONE');
return;
})
.catch((error) => {
console.log('Status ', error.status, ' - ', error.message);
return;
});