TL;DR
Can't get an async/await
function to do what an async
function achieves by returning a custom new Promise
object.
I'm trying to build a function that takes a string, loops through the words inside the string, and set an interval that logs each word after the set interval you assigned. Once it's done logging, a callback will log the total amount of words after the function has finish logging each of them. Below, you have the main function.
async function textReader (text, callback, interval = 1000) {
return new Promise((resolve, reject) => {
let counter = 0
let textArray = text.trim().split(' ')
let idInterval = setInterval( () => {
if(counter == textArray.length) {
callback(textArray.length)
clearInterval(idInterval)
resolve();
} else {
console.log(textArray[counter++])
}
}, interval)
})
}
Then, the callback that logs the quantity of words displayed:
function funCallback (wordQuantity) {
console.log(`Process complete - The text contains ${wordQuantity} words`)
}
And finally, an async
function that tests the main function. It simply runs 3 times the main function and log each of them, one after the other, as it is supposed to be. The idea is that each await
blocks the process until there is a resolved value (which actually means: while it is logging in the terminal each word), and once that await
is done, jump to the next textReader
function and so on.
async function test () {
try {
let time = 500
await textReader('When I find myself in times of trouble ', funCallback, time)
await textReader('mother Mary comes to me ', funCallback, time)
await textReader('speaking words of wisdom Let it be', funCallback, time)
} catch (err) { console.log(err)}
}
My issue is that I want the textReader
function to be able to achieve the same behavior without having to return a new Promise
, but using await
(because I guess that's what an async/await
function should be helpful with, right? achieve the same as an ES6 Promise
)
Please have in mind that the goals of the whole program are to:
- Log the words in a specific interval
- If
test()
holds more than onetextReader()
, they have to be blocking, meaning that one has to wait for the other to finish logging its words, otherwise the words from all the tested functions would overlap one over the other - which would be very confusing BTW -. - Count how many words were logged in each string.
I just don't see how can it be solved without having to return from textReader()
a new Promise
but using await
, as it should be in a async/await
function.
One of the attempts to solve it with async/await
(see below) didn't work; it just runs the 3 textReader()
from test()
functions all at the same time, overlapping the logs.
async function textReader (text, callback, time = 1000) {
let counter = 0
let textArray = text.trim().split(' ')
let loggingWords = async () => {
let idInterval = setInterval( () => {
if(counter == textArray.length) {
callback(textArray.length)
clearInterval(idInterval)
} else {
console.log(textoArray[contador++])
}
}, time)
}
let waitForLogingWords = await loggingWords()
return waitForLogingWords
};