Skip to content

Instantly share code, notes, and snippets.

@ThomasBurleson
Last active January 14, 2020 04:45
Show Gist options
  • Save ThomasBurleson/653810b0c39fc013de8b53c96198d4af to your computer and use it in GitHub Desktop.
Save ThomasBurleson/653810b0c39fc013de8b53c96198d4af to your computer and use it in GitHub Desktop.
export interface ResponseError = [any, Error];	// Tuple to return either 

/**
 * special promise trap to build tuple of reponse and error
 */
const trap = (promise): Promise<ResponseError> => {  
  return promise
    .then(data => ([data, undefined]))
    .catch(error => Promise.resolve([undefined, error]));
}

async assignUserToTask(taskId: string, user: User): ResponseError {
  const [url, body, options] = buildParams(taskId, user);

  this.updateState({ loading: true });       // start loading
  
  const request$ =this.http.put<Task>( url, body, options);
  const [updated, error] = await trap( request$.toPromise() );

  this.updateState({ loading: false });       // stop loading
  
  return [ updated, error ];	
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment