Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript/TypeScript: How to run code directly after the return statement in the same scope?

First of all: Is it possible? I have a vague memory about this being possible but I cannot seem to formulate my question well enough for google / stack to give me an idea of what to do.

Given this code (transform function within a custom pipe -Angular 2-):

transform(statuses: Status[], test: string): Status[] {

    return statuses.filter(status => status.Id === 1);

    //Code to be executed after return has happened
    this.updatePager.emit();
  }   

How do I get the code this.updatePager.emit();to happen right after my return statement.

Kind regards everyone. Tips on how to formulate this question so I can search further are always welcome!

like image 536
Jan Somers JanS91 Avatar asked May 19 '26 10:05

Jan Somers JanS91


2 Answers

This should work.

  transform(statuses: Status[], test: string): Status[] {

    setTimeout(function(){ this.updatePager.emit(); }, 1000); //Adjust time here.

    return statuses.filter(status => status.Id === 1);

  }   
like image 163
Brainmaniac Avatar answered May 21 '26 00:05

Brainmaniac


This might work for you, execute code in the next application tick with setTimeout 0.

transform(statuses: Status[], test: string): Status[] {
    //Code to be executed in the next tick
    setTimeout( ()=> this.updatePager.emit(), 0 );

    return statuses.filter(status => status.Id === 1);
}
like image 32
Val Avatar answered May 21 '26 00:05

Val



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!