Monthly Archives: October 2018

sending off questions and answers data

Regular Expressions (js)

new style

literal style

test

replace

Found “ryu” at position 6 in string “shooo ryu ken ryu Ryu rYu ryU!”
Found “ryu” at position 14 in string “shooo ryu ken ryu Ryu rYu ryU!”
Found “Ryu” at position 18 in string “shooo ryu ken ryu Ryu rYu ryU!”
Found “rYu” at position 22 in string “shooo ryu ken ryu Ryu rYu ryU!”
Found “ryU” at position 26 in string “shooo ryu ken ryu Ryu rYu ryU!”

After replacement: shooo ryu ken ryu ryu ryu ryu!

wait for async code in a callback function

Situation

We have a function that does some processing. Then it executes a callback function.

So say we call function A, and provide an anonymous callback function.

Under this case, we will get the true in function A’s definition.
Everything is synchronous so we go step by step.

The Problem

The problem happens when we have asynchronous code in our callback

Given definition A:

In our callback definition, we do something asynchrnous. It returns true after 2 seconds.

This is a problem because while the execution is happening for the setTimeOut of waiting 2 seconds, our main execution
continues and our reply will be undefined.

The Solution

The solution is to use Promises. In this particular case, I will use jQuery’s Deferred, which is exactly the same as Promise, except its packaged in jQuery’s API.

1) chain the ‘then’

In order to wait for the async to finish, we call the callback function, and then chain the API call ‘then’ in order to continue with the results. This is assuming we declared a Promise object and returned it in cbConfirmed(). (we’ll get to this in the next step)

When you chain the then call, it will wait for async code to finish, then you simply provide an anonymous callback function inside to get the results.

2) declare and return the Promise object

3) call resolve and use it where the async code has finished

The main execution will wait for the callback to finish. After 2 seconds, the callback returns and gives the result to function A.

quotes on love and relationship

每一个好的爱情,基于在尊重上。如果没有尊重,这个爱活不下去的。
每天要跟新你的爱。讲个笑话。。。带她出去玩。。。给他推拿。。。
结婚的目的,是教你怎么去做多情的朋友们。
爱情不会要求占有。爱情是给自由的。
你爱一个人,因为只有你能听到他生活上唱的歌。
微笑开始一个爱情。亲吻把爱情增长。拥抱让爱情持久。
爱是什么? 当两端有足够的空间时,爱就会让两个人坐在长凳的中间。