6 posts
by shahriyarnasir

ES7 introduces the async/await keywords. They are mostly syntactic sugar on top of Promises. You can write cleaner more readable asynchronous tasks code.

NOTE: They are available in Node 7.6.0 also.

Example:

Here is some code for programmatically creating a coding exercise for a developer candidate that has applied to us. We've improved the readability of it using async/await.

This hard to read test which uses Promises:

it('returns any errors that may have occurred',()=>{returnautomator.getRepo().then(repo=>!repo?automator.createExercise():Promise.resolve()).then(()=>this.room.user.say('alice',`hubotcreatecodingexerciserepofor${candidateEmail}`)).then(()=>{expect(lastBotMessage()).to.contain(`Errorcreatingcodingexercisefor*${candidateEmail}*:`)});});

Becomes this:

it('returns any errors that may have occurred',async()=>{letrepo=awaitautomator.getRepo();if(!repo){awaitautomator.createExercise();}awaitthis.room.user.say('alice',`hubotcreatecodingexerciserepofor${candidateEmail}`);expect(lastBotMessage()).to.contain(`Errorcreatingcodingexercisefor*${candidateEmail}*:`)});

The Decorator Pattern allows us to chain new behaviours to objects without modifying the underlying objects. It is an application of the Open/Closed Principle. This pattern is useful for example when we need to tack on logging, monitoring, and other non-functional requirements to objects.

In Java or C# this can be achieved using interfaces. In Ruby, we can use the SimpleDelegator class to achieve this:

It would appear that the colon in git push origin :<branch-to-delete> is used exclusively to delete branches. But such is not the case.

The format for the refspec is*:

<source>:<destination>

This tells Git to push the source branch to the destination branch in remote. So if the source is blank, we get a leading colon. This has the effect of deleting the destination branch. Its like saying push null pointer to destination.

*You can learn more about the refspec in its entirety in this Stack Overflow

Execute the profiler in a watch session every minute: watch -n 60 --no-title "./profiler.sh SCRIPT_IDENTIFIER | tee -a logfile". Where the script identifier is any text that we can use to grep for the process in the ps aux output.

After your script is done running or you have enough data points, observe the output in logfile.

In integration specs, it is preferable to call the original method when setting up an expectation on an object to receive an invocation of that method. This way, the method isn't stubbed out but instead will still be invoked. Any downstream effects of calling that method won't be hidden.

classCalculatordefself.add(x,y)x+yendend

Should be tested like this:

require'calculator'RSpec.describe"and_call_original"doit"responds as it normally would"doexpect(Calculator).toreceive(:add).and_call_originalexpect(Calculator.add(2,3)).toeq(5)# any bugs inside of #add won't be hiddenendend

TIL is a Nulogy microblog for sharing web development tips, tricks, and tidbits. From Ruby on Rails to AngularJS, our Engineering department blogs on topics across the stack to share in our collective success and improve our understanding of all things technology.