0

I try use yammer SDK https://c64.assets-yammer.com/assets/platform_js_sdk.js to make async call like the code below.

The javascript SDK doc is here https://developer.yammer.com/docs/js-sdk

What the code currently does is to return an Array of 50 users' profile. The total number of users is unpredictable.

What I want: When the returned array.length in previous call is equal to 50, i.e. there could be more users in following page, make another call with index++ to the same API URL.

This is repeated until there is no more users to be fetched.

But how to make it?

yam.connect.loginButton('#yammer-login', function (resp) { console.log(resp.authResponse); var index = 1; if (resp.authResponse) { //trigger data process yam.platform.request({ url: "users.json",
method: "GET", data: {
"page": index }, success: function (user) { console.log("The request was successful."); console.log(user.length); }, error: function (user) { console.log("There was an error with the request."); } }); }else{ console.log("error to get access_token"); } });

1 Answer 1

1

Simply by creating a getUsers function and a global variable (in this case I've scoped it all through an immediately-invoked function) to control index you can just check if users length is 50, and if so - run the function again:

(function() {
    var index = 1;

    var getUsers = function() {
        yam.platform.request({
            url: "users.json",
            method: "GET",
            data: {
                "page": index
            },
            success: function (user) { 
                console.log("The request was successful.");
                console.log(user.length);

                if (user.length === 50) {
                    // There are additonal users - increment index and run the function again
                    index++;

                    getUsers();
                }
            },
            error: function (user) {
                console.log("There was an error with the request.");
            }
        });
    };

    yam.connect.loginButton('#yammer-login', function (resp) {
        console.log(resp.authResponse);

        if (resp.authResponse) {
            //trigger data process
            getUsers();
        } else {
            console.log("error to get access_token");
        }
    });
})();
Sign up to request clarification or add additional context in comments.

5 Comments

elegant! Works like a charm, I was not confident about promise and didnt know how to wrap the call into a function... The result turned to be not complicated...
what if the call is established with call().then() format, is it similar to this answer? I really need to learn well promise first...
@XiXiao I'm unaware of how yam creates promises - could you update your OP?
I dont quite know either. I added the SDK intro doc URL. Microsoft has poor documentation around it, it seems the referenced js minified file is the only resource to check code..
@XiXiao Could you try out the following? My Promise knownledge is not particularly up to date, but it should work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.