Real-Serious-Games / C-Sharp-Promise

Promises library for C# for management of asynchronous operations.
MIT License
1.19k stars 149 forks source link

Run multiple promises, one after the other #70

Closed nah0y closed 6 years ago

nah0y commented 6 years ago

Hello again :)

I have this bit of code:

IPromise[] promiseArray = new IPromise[packetCount];
for (int i = 0; i < packetCount; i++)
{
    promiseArray[i] = WriteCharacteristicInternal(byteArray.Skip(i * 20).Take(20).ToArray());
}

return Promise.All(promiseArray);

But as the doc says, it will run all my promises in parallel. Also, it seems that even if I don't call WriteCharacteristicInternal, it will trigger the promise. So I don't really understand why in the doc it says that Promise.All will run them all in parallel, as they are already started just by calling the WriteCharacteristicInternal method. Anyway :) Is there a "native" way of saying that I want to run an array of promises in sequence? Waiting for one to finish, then start the other etc... without having to do that "manually" in multiple .Then?

Thanks!

ashleydavis commented 6 years ago

Use Promise.Race.

You can read about it in my article: http://www.what-could-possibly-go-wrong.com/promises-for-game-development/

nah0y commented 6 years ago

"Race is similar to All, but it resolves immediately when the first inner promise resolves. Each of the promises are racing each other for completion." This is not what I want. I want all of my promises to resolve, but just the first one first, then the second, then the third etc...

ashleydavis commented 6 years ago

Sorry I meant Promise.Sequence!! Again check my article.

I haven't used C# promises for almost a year now!

nah0y commented 6 years ago

Seems like what I'm looking for thanks! Again sorry, in my head there were only All and Race...