S
S
sinevik2018-03-09 20:35:16
Mongoose
sinevik, 2018-03-09 20:35:16

Can't figure out the shortcode?

I'm watching American courses, help me understand the short code

const expect = require('expect');
const request = require('supertest');
const {ObjectID} = require('mongodb');

const {app} = require('./../server');
const {Todo} = require('./../models/todo');



beforeEach((done) => {
  Todo.remove({}).then(() => {
    return Todo.insertMany(todos);
  }).then(() => done());
});


У нас задействован Promise. Подскажите где тут у нас функция reject и resolve. Если нет resolve, где тогда catch? И что такое done?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim, 2018-03-10
@sinevik

Judging by the code, we are talking about testing.
You are describing the callback inside the beforeEach function .
Callback is a function that will be called at some point, conceived by the author, therefore the author also conceived that an argument will be passed inside this "callback" - another function. You called it done (because everyone calls it that, but instead of done , you could write qwe and the same would work).
Resolve and reject are names (also made up for convenience) of functions that you pass to then:

beforeEach((done) => {
  Todo.remove({}).then(() => {
    return Todo.insertMany(todos);
  }).then(resolve, reject);
});

It turns out that your resolve is the first argument, that is, the anonymous function, the first in then , that is:
The reject function is not here, just as there is no catch function .
Example, about qwe:
beforeEach((qwe) => {
  Todo.remove({}).then(() => {
    return Todo.insertMany(todos);
  }).then(() => qwe());
});

https://learn.javascript.ru/promise

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question