Can asynchronous calls be converted into synchronous calls in JS?

function wait() {
        const p = () => ({
            value: new Promise(resolve => setTimeout(() => resolve(), 3000))
        })
        let state = {
            next: () => {
                state.next = programPart
                return p()
            }
        }
        function programPart() {
            console.log("unlocked1")
            state.next = programPart2
            return p()
        }
        function programPart2() {
            console.log("unlocked2")
            console.log("it\"s sync!!")
            return {
                value: void 0
            }
        }
        return state
    }

how should this function be called?

Apr.12,2022

var state = wait()
state.next()
    .value
    .then(() => state.next().value)
    .then(() => state.next().value)
    .then(() => console.log(state.next()))

you can use the async function to visually synchronize calls. Async is the syntactic sugar of the Generator function.

const fn = async function () {
  let state = wait();
  await state.next().value;
  await state.next().value;
  return state.next().value;
}
fn()
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b3c6d1-2c2f3.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b3c6d1-2c2f3.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?