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()
Menu