Skip to main content
added 102 characters in body
Source Link
user99319
user99319

It appears there may be some confusion regarding promises. From Mozilla's Promise.All documentation:

The Promise.all(iterable) method returns a single Promise that resolves when all of the promises in the iterable argument have resolved or when the iterable argument contains no promises. It rejects with the reason of the first promise that rejects.

In your case you are using Promise.All to ensure your game has fully loaded, specifically that all functions returned by Game.load have completed. However in order for this to work each of these functions must return a promise.

The code hasn't been provided for AssetLoader.LoadImage, but looking at LoadGame it is clear that no promise is returned - in fact, nothing is returned.

However because JQuery's getJSON function return a jqXHR object, which implemnets the Promise interface, you can simply make LoadGame return the results of your $.getJSON call. For example:

function LoadGame(gameid) {
    // Note we're returning the Promise.
    return $.getJSON('/api/game/' + gameid)
    .done(function (game) {
        if (game !== null) {
            mapid = game.MapId;
            map = AssetLoader.LoadMapSection(mapid, 0);
        }
        else
            console.log("Could not load game with id: " + gameid);
    })
    .fail(function (jqXHR, status, error) {
        console.log(error);
    });
}

You should also ensure thatAdditionally because AssetLoader.LoadImageLoadMapSection returns a Promiseis also asynchronous you'll need to add this change to that method aswell, and any other nested functions which are asynchronous.

It appears there may be some confusion regarding promises. From Mozilla's Promise.All documentation:

The Promise.all(iterable) method returns a single Promise that resolves when all of the promises in the iterable argument have resolved or when the iterable argument contains no promises. It rejects with the reason of the first promise that rejects.

In your case you are using Promise.All to ensure your game has fully loaded, specifically that all functions returned by Game.load have completed. However in order for this to work each of these functions must return a promise.

The code hasn't been provided for AssetLoader.LoadImage, but looking at LoadGame it is clear that no promise is returned - in fact, nothing is returned.

However because JQuery's getJSON function return a jqXHR object, which implemnets the Promise interface, you can simply make LoadGame return the results of your $.getJSON call. For example:

function LoadGame(gameid) {
    // Note we're returning the Promise.
    return $.getJSON('/api/game/' + gameid)
    .done(function (game) {
        if (game !== null) {
            mapid = game.MapId;
            map = AssetLoader.LoadMapSection(mapid, 0);
        }
        else
            console.log("Could not load game with id: " + gameid);
    })
    .fail(function (jqXHR, status, error) {
        console.log(error);
    });
}

You should also ensure that AssetLoader.LoadImage returns a Promise aswell.

It appears there may be some confusion regarding promises. From Mozilla's Promise.All documentation:

The Promise.all(iterable) method returns a single Promise that resolves when all of the promises in the iterable argument have resolved or when the iterable argument contains no promises. It rejects with the reason of the first promise that rejects.

In your case you are using Promise.All to ensure your game has fully loaded, specifically that all functions returned by Game.load have completed. However in order for this to work each of these functions must return a promise.

The code hasn't been provided for AssetLoader.LoadImage, but looking at LoadGame it is clear that no promise is returned - in fact, nothing is returned.

However because JQuery's getJSON function return a jqXHR object, which implemnets the Promise interface, you can simply make LoadGame return the results of your $.getJSON call. For example:

function LoadGame(gameid) {
    // Note we're returning the Promise.
    return $.getJSON('/api/game/' + gameid)
    .done(function (game) {
        if (game !== null) {
            mapid = game.MapId;
            map = AssetLoader.LoadMapSection(mapid, 0);
        }
        else
            console.log("Could not load game with id: " + gameid);
    })
    .fail(function (jqXHR, status, error) {
        console.log(error);
    });
}

Additionally because AssetLoader.LoadMapSection is also asynchronous you'll need to add this change to that method aswell, and any other nested functions which are asynchronous.

Source Link
user99319
user99319

It appears there may be some confusion regarding promises. From Mozilla's Promise.All documentation:

The Promise.all(iterable) method returns a single Promise that resolves when all of the promises in the iterable argument have resolved or when the iterable argument contains no promises. It rejects with the reason of the first promise that rejects.

In your case you are using Promise.All to ensure your game has fully loaded, specifically that all functions returned by Game.load have completed. However in order for this to work each of these functions must return a promise.

The code hasn't been provided for AssetLoader.LoadImage, but looking at LoadGame it is clear that no promise is returned - in fact, nothing is returned.

However because JQuery's getJSON function return a jqXHR object, which implemnets the Promise interface, you can simply make LoadGame return the results of your $.getJSON call. For example:

function LoadGame(gameid) {
    // Note we're returning the Promise.
    return $.getJSON('/api/game/' + gameid)
    .done(function (game) {
        if (game !== null) {
            mapid = game.MapId;
            map = AssetLoader.LoadMapSection(mapid, 0);
        }
        else
            console.log("Could not load game with id: " + gameid);
    })
    .fail(function (jqXHR, status, error) {
        console.log(error);
    });
}

You should also ensure that AssetLoader.LoadImage returns a Promise aswell.