Can I get a jQuery Deferred on document.ready()?

后端 未结 6 1414
失恋的感觉
失恋的感觉 2020-12-14 07:47

Right after my script is loaded I am making an Ajax request to get some translations. This should always return after the document is ready since I am loading my scripts at

6条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-14 08:37

    jQuery's when is not a proper promise. You can force it into one like this:

    function documentReady() {
        return Promise.resolve($.when($.ready));
    }
    

    Usage:

    documentReady().then(function($) { ... });
    

    It happens to resolve with $ so that's kind of convenient too.

    Alternative implementation:

    function documentReady() {
        return new Promise(r => $(r));
    }
    

提交回复
热议问题