Promises, client.auth.client(), & appToken.get(...) / .post(...)

I have a couple of questions about Dwolla’s thenable api.

  1. Does client.auth.client() return a promise? What happens if I chain .then() to it many times?

  2. Is appToken a promise? What happens if I chain .then() to it many times?

  3. Question on the asynchronous nature of the API. Suppose I’m trying to remove a funding source and adding a new one with duplicate info. What happens if I attempt to create a duplicate funding source (same bank account) before the funding source can be removed? How do I “wait” for all of a customer’s funding sources to be removed before attempting to add a new funding source?

If I add a .then() to client.auth.client() after appToken.post(…).then(…), is there a way I can guarantee it to run after the appToken “post” request as completely finished and the “then” callback after appToken has finished running? I mean like in the following:

client.auth.client()
.then(appToken => {
	return appToken.post(".....")
	.then(resp => {
		// do something with resp
	})
}).then()    <--- this is the "then" I'm talking about; is there a way
to ensure that this runs last?

Suppose I have an array, of arbitrary length, of funding source URLs that I want to “remove.” Is there a way I can have a callback/.then() that is guaranteed to run AFTER all of funding sources are finished being removed? On the other hand, is there a way I can get a callback to run IMMEDIATELY after the last funding source has been removed?

let fundingSources = [...];
client.auth.client()
.then(appToken => {
	return appToken.post(".....", {removed: true})
	.then(resp => {
		// do something with resp
	});
});

Hey Scott,

  1. Yes, client.auth.client() does return a Promise
  2. appToken is not a Promise (but it is the value resolved by client.auth.client()'s Promise)
  3. I believe the part in your example you want to ensure runs last will run last. (Similar to Example 1 below)
  4. I think the code in Example 2 below should work. You may run into rate limiting though depending on how big the list of funding sources is.

Example 1:

Promise.resolve()
  .then(function() {
    return Promise.resolve().then(function() {
      console.log("a");
    });
  })
  .then(function() {
    console.log("b");
  });

Example 2:

var fsUrls = [...];

Promise.all(
  fsUrls.map(function(url) {
    return appToken.post(url, { removed: true });
  })
).then(function() {
  // ...
});
2 Likes

Thank you very much! This works for me.

1 Like