2

Is there a name for this pattern? I've written this kind of thing a few times in JavaScript and recently found myself writing it in C#. Thing is, I expect this has already been implemented in a library somewhere I didn't know what to look for.

In pseudo code it's something like this:

if (null != referenceToPreviousRequest) {
    referenceToPreviousRequest.cancel();
}

referenceToPreviousRequest = setTimeout(function() {
    //The thing I ultimately want to do
}, 1000 /*The delay after the last request*/);

Typically I use this kind of code in client side JavaScript, for delaying an AJAX request until a certain amount of time after the last user input, e.g. searching.

3

What you describe are known as JavaScript Promises, in general programming parlance they are called Continuations. C# supports them through the Task Parallel Library and Async/Await keywords.

Here's how it works, I want to call an operation asynchronously. If the operation is already declared Async it's simple:

  • Add async to your function that is calling the existing async function
  • Change the return value to Task (for void) or Task (for any other type T)
  • Place the keyword await in front of the call to that async function. And follow that call with the rest of my function's logic.

Here's an example (taken from MSDN Docs on Async Await).

// Three things to note in the signature: 
//  - The method has an async modifier.  
//  - The return type is Task or Task<T>. (See "Return Types" section.)
//    Here, it is Task<int> because the return statement returns an integer. 
//  - The method name ends in "Async."

async Task<int> AccessTheWebAsync()
{ 
    // You need to add a reference to System.Net.Http to declare client.
    HttpClient client = new HttpClient();

    // GetStringAsync returns a Task<string>. That means that when you await the 
    // task you'll get a string (urlContents).
    // The await operator suspends AccessTheWebAsync. 
    //  - AccessTheWebAsync can't continue until getStringTask is complete. 
    //  - Meanwhile, control returns to the caller of AccessTheWebAsync. 
    //  - Control resumes here when getStringTask is complete.  
    //  - The await operator then retrieves the string result from getStringTask. 

    string urlContents = await client.GetStringAsync("http://www.microsoft.com");

    // The return statement specifies an integer result. 
    // Any methods that are awaiting AccessTheWebAsync retrieve the length value. 
    return urlContents.Length;
}

Reading the linked article will describe what's happening in the background.

You can also explicitly perform continuations in C# using the ContinueWith operation

  • According to this EMCAScript draft, future versions of Javascript will probably have native promise support. – Brian Nov 24 '14 at 19:37
  • Hmmm, I'm not convinced. I'm aware of promises, but the key part of the pattern I'm describing is multiple invocations/requests but only one actual run, after a set delay after the last invocation. While this can be built with promises it's not intrinsic to the concept of promises, as far as I'm aware. – Ian Newson Nov 25 '14 at 10:31
  • Oh and crucially, I don't care about the result of the operation, just that it happens eventually. – Ian Newson Nov 26 '14 at 21:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.