简体   繁体   English

如何将回调样本转换为延迟对象?

[英]How to convert callback sample to deferred object?

I have a function that accepts a callback function where I pass the data back in. Can this converted to a deferred object for better practice? 我有一个接受回调函数的函数,我将数据传回来。这可以转换为延迟对象以便更好地练习吗?

Here is what I got: 这是我得到的:

    var chapters;
    var getChapters = function (fnLoad) {
        //CACHE DATA IF APPLICABLE
        if (!chapters) {
            //CALL JSON DATA VIA AJAX
            $.getJSON('/chapters.txt')
                .done(function (json) {
                    //STORE DATA IN LOCAL STORAGE
                    chapters = Lawnchair(function () {
                        this.save(json, function (data) {
                            //CALL CALLBACK ON DATA
                            fnLoad(data);
                        });
                    });
                });
        } else {
            //RETURN ALREADY CREATED LOCAL STORAGE
            chapters.all(function (data) {
                //CALL CALLBACK ON DATA
                fnLoad(data);
            });
        }
    };

Then I simply use it like this: 然后我就这样使用它:

this.getChapters(function (data) {
    console.log(data);
});

How can I use it like a promise though while maintaining the cache approach? 虽然在维护缓存方法的同时如何使用它像一个承诺?

this.getChapters().done(function (data) {
    console.log(data);
});
var chapters;
var getChapters = function (fnLoad) {
    var d = new $.Deferred();
    //CACHE DATA IF APPLICABLE
    if (!chapters) {
        //CALL JSON DATA VIA AJAX
        $.getJSON('/chapters.txt')
            .done(function (json) {
                //STORE DATA IN LOCAL STORAGE
                chapters = Lawnchair(function () {
                    this.save(json, function (data) {
                        //CALL CALLBACK ON DATA
                        d.resolve(data);
                    });
                });
            })
            .fail(function() { d.reject(); });
    } else {
        //RETURN ALREADY CREATED LOCAL STORAGE
        chapters.all(function (data) {
            //CALL CALLBACK ON DATA
            d.resolve(data);
        });
    }
    return d.promise();
};

Relevant example 相关的例子

I see you have already accepted an answer, however if you take a large mental leap and store a promise of chapters instead of the chapters themselves, then the code will simplify significantly. 我看到你已经接受了一个答案,但是如果你进行了大量的精神飞跃并存储了chapters的承诺而不是chapters本身,那么代码将会大大简化。

These days, this is probably the more generally adopted approach for a "fetch/cache" situation. 这些天,这可能是更常用的“获取/缓存”情况的方法。

var chapters_promise;
var getChapters = function () {
    //Cache data if applicable and return promise of data
    if (!chapters_promise)
        chapters_promise = $.getJSON('/chapters.txt').then(Lawnchair).then(this.save);
    return chapters_promise;
};

What is actually promised (the chapters) will be determined by the value(s) returned by the functions Lawnchair and this.save , so you still have some work to do. 实际承诺的内容(章节)将由Lawnchairthis.save函数返回的值确定,因此您仍有一些工作要做。

getChapters() will always return a promise, regardless of whether the data needs to be fetched or is already cached. getChapters()将始终返回一个promise,无论数据是需要获取还是已经缓存。 Therefore, getChapters() can only be used with promise methods .then() , .done() , .fail() or .always() , for example : 因此, getChapters()只能与承诺方法中使用.then() .done() .fail().always()例如:

getChapters().then(fnLoad);

You have no other way to access the chapters but that is reasonable since at any call of getChapters() , you don't know whether it will follow the $.getJSON() branch or the simple return branch, both of which return an identical promise. 你没有其他方法可以访问这些chapters但这是合理的,因为在任何getChapters()调用时,你都不知道它是否会遵循$.getJSON()分支或简单的return分支,两者都返回相同的诺言。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM