Z
Z
zekohina2016-11-23 21:18:27
JavaScript
zekohina, 2016-11-23 21:18:27

How to build an architecture with asynchrony in mind?

Asynchrony in js is both good and bad.
Let's say I make a validator that takes data and validation rules, and returns either a list of validation errors, or null (in case of success). Something like that:

validator(
data,
{
'email': ['email', 'required'],
'password': [{string: {min: 8, max: 50}}, 'required'],
'phone': [{number: {length: 11}}]
}
)

And everything would be fine. But as soon as you need to validate email on a real existence, you will have to make a request, and this is already an asynchronous operation, and simply returning null will not work. You need to return either a promise or a callback, and this is rewriting a bunch of code.
Actually the question is, how to be in such a situation? How to plan architecture so that it doesn't hurt? Always return a promise so that it is for the future?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
Nicholas, 2016-11-23
@healqq

Well, yes, return a promise.
Well, or immediately look into the future in the direction of co and async / await.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question