This question already has answers here:
async/await implicitly returns promise?
(5 answers)
Why is my asynchronous function returning Promise { <pending> } instead of a value?
(9 answers)
Async function returning promise, instead of value
(3 answers)
How can I access the value of a promise?
(14 answers)
What are asynchronous functions in JavaScript? What is "async" and "await" in JavaScript?
(2 answers)
Closed 4 months ago.
static async updatePaidLeave() {
const result = await PaidUnpaidLeaveSchema.find({});
console.log(result, ";;;;;;;");
}
static runCronJob() {
cron.schedule("*/10 * * * * *", async () => {
this.updatePaidLeave();
console.log(this.updatePaidLeave(), "Im running in every 10 seconds.");
});
}
Output // Promise { } Im running in every 10 seconds.
i have tried above code and i want data from PaidUnpaidLeaveSchema
The code requires a couple of fixes. Try this (look at comments):
static async updatePaidLeave() {
const result = await PaidUnpaidLeaveSchema.find({});
console.log(result, ";;;;;;;");
return result // return result
}
static runCronJob() {
cron.schedule("*/10 * * * * *", async () => {
const result = await this.updatePaidLeave(); // await and save result in a variable
console.log(result, "Im running in every 10 seconds."); // log result
});
}
Related
This question already has answers here:
Resolve promises one after another (i.e. in sequence)?
(36 answers)
Is Node.js native Promise.all processing in parallel or sequentially?
(14 answers)
Closed 6 months ago.
I asked a question that was closed due to similar questions but even after reading those and seeing that map is not Promise aware, I have tried to use await Promise.all() around my mapping.
let foo = [1,2,3];
(async() => {
await Promise.all(foo.map(x => {
return new Promise(async function(resolve) {
await setTimeout(() => {
console.log("X")
resolve()
}, 3000)
});
}))
})()
instead of sequentially getting a "X" log every 3 seconds, it's just waiting 3 seconds and then logging them all. I am not sure what I am doing wrong at this point.
You are setting all the timeouts together at once. You need to increase the timeout duration in each iteration, maybe like this:
let foo = [1,2,3];
(async() => {
await Promise.all(foo.map((x, i) => {
return new Promise(async function(resolve) {
await setTimeout(() => {
console.log("X")
resolve()
}, 3000 * (i + 1))
});
}))
})()
This question already has answers here:
async/await implicitly returns promise?
(5 answers)
Why is my asynchronous function returning Promise { <pending> } instead of a value?
(9 answers)
Async function returning promise, instead of value
(3 answers)
How can I access the value of a promise?
(14 answers)
What are asynchronous functions in JavaScript? What is "async" and "await" in JavaScript?
(2 answers)
Closed 8 months ago.
const wordEl = document.getElementById('word');
let selectedWord = [];
let correctLetters = [];
let apiUrl = 'https://random-word-api.herokuapp.com/word';
async function getData(url) {
let response = await fetch(url);
let data = await response.json();
selectedWord = await data;
}
selectedWord = getData(apiUrl);
async function displayWord() {
wordEl.innerHTML = `
${selectedWord
.toString()
.split('')
.map(
(letter) =>
`
<span class="letter">
${correctLetters.includes(letter) ? letter : ''}
</span>
`
)
.join('')}
`;
}
window.addEventListener('keydown', (e) => {
if (selectedWord.toString().includes(e.key)) {
if (!correctLetters.includes(e.key)) {
correctLetters.push(e.key);
displayWord();
}
}
});
displayWord();
Hello my first value returning [object promise]. After pressing any key value returning what I want.
How can i get normal value before clicking any key? Thank you!
This question already has answers here:
When should I use a return statement in ES6 arrow functions
(6 answers)
How to return many Promises and wait for them all before doing other stuff
(6 answers)
Closed 2 years ago.
Why protocols.usd is always returning 0?
I try making a dummy promise function to test it and it's working properly but it's not working with the await function.
async function addPrices() {
const balances = await getBalances();
await Promise.all(balances.map(protocols => {
protocols.usd = 0;
protocols.balances.map(balance => {
if (balance.underlying) {
balance.underlying.map(async token => {
let price = await getPrice(token.address);
protocols.usd += price.market_data.current_price.usd;
});
}
});
}));
return balances;
}
This question already has answers here:
NodeJS Timeout a Promise if failed to complete in time
(8 answers)
Closed 2 years ago.
I am waiting for my promise to resolve in an async function using await but I want to wait for only 20 sec. If no response comes in that time (neither positive nor negative from the promise), I want to continue and display 'timeout'. How can I do this?
You can use Promise.race:
const promise1 = func();
const promise2 = new Promise((res, rej) => setTimeout(rej, 20000));
try {
await Promise.race([promise1, promise2]);
} catch (e) {
// time out or func failed
}
This question already has answers here:
How to use promises in javascript
(2 answers)
How do I return the response from an asynchronous call?
(41 answers)
Closed 3 years ago.
I am using an ajax to get some of my data from database without refreshing my page. So made this function:
function setElapsedDays(contract){
...
let diff_in_time = end_date.getTime() - start_date.getTime();
let days_elapsed = diff_in_time / (1000 * 3600 * 24);
console.log(this.getVacationEntitlment(contract.id));
let accrued = this.getVacationEntitlment(contract.id) / days_elapsed;
console.log(accrued);
}
async function getTravelEntitlment(contract_id){
let url = window.location;
const response = await fetch(url.origin+"/api/benefits/search/2/"+contract_id);
const object = await response.json();
return object.allotment;
}
but why i am getting NaN on my accrued?
Using console.log(this.getVacationEntitlment(contract.id)); I am getting
Promise {<pending>}
__proto__: Promise
[[PromiseStatus]]: "resolved"
[[PromiseValue]]: 30
If i console.log() my object i get this {id: 2, contract_id: 736, benefit_id: 2, allotment: 30, uom: "Days", …}
How can i get the value of PromiseValue?