Skip to content

Instantly share code, notes, and snippets.

@lsmith
Last active December 15, 2015 14:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lsmith/5271961 to your computer and use it in GitHub Desktop.
Save lsmith/5271961 to your computer and use it in GitHub Desktop.
var dataPromise = new Y.Promise(function (resolve, reject) {
var data = [];
function getData(offset) {
return new Y.Promise(function (dataResolve, dataReject) {
Y.io('getdata.php?offset=' + offset, {
on: {
success: function (id, response) {
var dataset = Y.JSON.parse(response.responseText);
data.push.apply(data, dataset);
dataResolve((dataset.length < 100) ? data : getData(offset + 100));
},
failure: function () {
dataReject(new Error("Oh noes!"));
}
}
});
});
}
getData(0).then(resolve, reject);
});
dataPromise.then(function (allTheData) {
// allTheData has ... all of the data
}, handleError);
@ItsAsbreuk
Copy link

Luke, suppose I create the next Promise.
Will there be a memoryleak? It seem to me that there is a closure (options-object) that could prevent every created 'getData' from being freed from memory.

Am I right? Or isn't it?

function promiseWithOptions(options) {
    return new Y.Promise(function (resolve, reject) {
        var data = [];

        function getData(offset) {
            return new Y.Promise(function (dataResolve, dataReject) {
                Y.io(options.url + '?offset=' + offset, {
                    on: {
                        success: function (id, response) {
                            var dataset = Y.JSON.parse(response.responseText);

                            data.push.apply(data, dataset);

                            dataResolve((dataset.length < 100) ? data : getData(offset+100));
                        },
                        failure: function () {
                            dataReject(new Error("Oh noes!"));
                        }
                    }
                });
            });
        }

        getData(0).then(resolve, reject);
    });
}

var options = {url: 'getdata.php'};
var dataPromise = promiseWithOptions(options);

dataPromise.then(function (allTheData) {
    // allTheData has ... all of the data
}, handleError);

Marco.

@lsmith
Copy link
Author

lsmith commented May 2, 2013

I don't know for certain, since garbage memory leakage and garbage collection isn't my specialty, but I believe it will be fine. When the inner promise resolves, the getData() code path will go stale and shouldn't prevent the related promises from being GC'd. Once the exposed promise is not being used, I don't see why it can'be GC'd.

But again, it's not my specialty. I really do need to learn more about memory leaks and garbage collection...

@ItsAsbreuk
Copy link

I thought you knew everything ;)

You may be right.
On irc we discussed this as well and all say no memoryleaks to be expected.
I think I messed up: the promise may hold options, but GC issues would be expected the other way arround (when an external object were to hold the promise). At least, I think so...

Thx.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment