cocos / cocos-engine

Cocos simplifies game creation and distribution with Cocos Creator, a free, open-source, cross-platform game engine. Empowering millions of developers to create high-performance, engaging 2D/3D games and instant web entertainment.
https://www.cocos.com/en/creator
Other
8.21k stars 1.94k forks source link

Provide unified Promise API #10747

Open jareguo opened 2 years ago

jareguo commented 2 years ago

It's 2022, and it's time to evaluate fully enabling Promise. From a compatibility perspective, a more general solution is to determine whether to use Promise based on the callback function passed in. Old style:

loadScene(foo, callback); // return nothing, behaviour unchanged

New style:

await loadScene(foo); // return Promise when callback unspecified

From https://forum.cocos.org/t/creator/82949 See also image

Ronsku commented 2 years ago

Like mentioned on Trello side and in the screenshot above: Would it be possible to make functions like instantiate(prefab) asynchronous as well? and maybe even functions like node.addNode(...) / node.destroy()? Especially instantiate() can sometimes take a a while to instantiate a bigger prefab on a weak device. I know there are workaround like adding a component on the prefab and running onLoad() or such function to see when it is ready, but it would help a lot to know when the prefab you instantiated is ready just by await instantiate(prefab), so clean.