#js #interview #promise
Problem: Implement a simple version of Promise:
The Promise object is an asynchronous programming solution for handling asynchronous events. Promise objects can represent the status of an asynchronous operation, including:
- pending
- fulfilled
- rejected
Analysis:
๐ถThe MyPromise class is a custom Promise class whose constructor accepts an executor function as a parameter.
๐ฝThe executor function in the constructor will be executed immediately and accepts two parameters, resolve and reject, which are used to modify the state of the Promise.
๐The resolve method is used to modify the Promiseโs status from โpendingโ to โfulfilledโ and pass the value to subsequent handlers.
๐ฅThe reject method is used to modify the Promiseโs status from โpendingโ to โrejectedโ and pass the reason to the subsequent handler.
๐ณThe then method is used to register a callback function to be executed when the Promise is completed or rejected. It accepts two parameters: onFulfilled and onRejected, which are called when the Promise is completed or rejected respectively.
๐ฅฉThe then method returns a new MyPromise instance to support chained calls. If onFulfilled or onRejected returns a value, it will be used as the resolved value for the next MyPromise instance.
๐The catch method is the shorthand form of then(null, onRejected).
๐The isFulfilled method is used to check whether the Promise is in the fulfilled state.
๐ฎThe isRejected method is used to check whether the Promise is in the rejected state.
Please open Telegram to view this post
VIEW IN TELEGRAM
Please open Telegram to view this post
VIEW IN TELEGRAM
๐4
This media is not supported in your browser
VIEW IN TELEGRAM
#js #interview #eventLoop
Question: Give an explanatory note on the event loop mechanism.
answer:
The event loop mechanism mainly has the following processes:
๐Synchronous tasks are executed on the main thread, forming an execution context stack.
๐Once all synchronous tasks in the execution stack have been executed, the system will read the asynchronous tasks in the queue, such as Promise.then(), setTimeout, AJAX callbacks, etc.
๐The asynchronous task will be added to the task queue
๐ฅOnce the execution stack is cleared, the system checks the task queue. If it is not empty, the first task is taken out and placed on the execution stack for execution.
๐ฅฆThe main thread repeats the process of alternating execution of the stack and queue, thereby realizing queued execution of threads.
The event loop allows synchronous tasks and asynchronous tasks to be executed alternately in the same thread, making full use of CPU resources. This is important for JavaScript that supports UI interaction and responsiveness.
Please open Telegram to view this post
VIEW IN TELEGRAM
โค1๐1
#js #interview #ajax
Implement an ajax request function that supports Promise:
๐ฃSend a request using the XMLHttpRequest object
๐Initialize the open method, configure the request method and url
๐ทAdd onload and onerror callback functions
๐งonload determines whether the status code is within the range of 200โ300 resolve, otherwise reject
๐ซonerror directly reject
๐ฅงAfter the request is successful, resolve returns response, and after failure, reject reports an error.
๐ฅSupport options to configure request parameters and request body
๐ฑReturns a Promise object, which can be processed externally using then/catch
Analysis: Promise is used to encapsulate asynchronous ajax requests and achieve a synchronous programming style.
Please open Telegram to view this post
VIEW IN TELEGRAM
#js #interview #jsonp
Analysis: Create the script node script.src, set the callback function callbackName, parse the parameters and splice the URL, dynamically insert it into the body to implement JSONP cross-domain request, and return the Promise interface.
Please open Telegram to view this post
VIEW IN TELEGRAM
#js #interview #deepClone
Analysis: Recursively implement deep cloning of objects and arrays, returning basic types directly, and reference types recursively call deep cloning hierarchically.
Please open Telegram to view this post
VIEW IN TELEGRAM
#js #interview #deepClone #patterns
Analysis: Currying of the add function is achieved by recursively calling a function that continues to accept parameters.
Please open Telegram to view this post
VIEW IN TELEGRAM
#js #interview #promise
Analysis: Use the Promise.all principle to synchronize the Promise state through the counter and result array.
Please open Telegram to view this post
VIEW IN TELEGRAM
#js #interview #instanceof
The instanceof operator in JavaScript can be used to determine whether an object is an instance of another object. However, the instanceof operator has some limitations, such as:
๐ฅซThe instanceof operator can only determine objects directly connected to the prototype chain.
๐ฏThe instanceof operator cannot detect objects with cyclic prototype chains.
Therefore, the above code provides a more general instanceof function that can determine the relationship between any two objects.
The implementation principle of this function is:
๐The function instanceof receives two parameters: left and right.
๐ฅFirst, the code checks to see if the number of parameters is 2, and if not, throws an error.
๐ฅNext, the code checks whether the left operand left is null. If so, it returns false directly, because null cannot be an instance of any object.
๐งThen, the code checks whether the type of the left operand left is an object. If not, it returns false directly, because only objects can be instances of the constructor.
๐ฅNext, the code uses Object.getPrototypeOf() to obtain the prototype of the left operand left and assigns it to the variable proto.
๐In a loop, the code continues to traverse protoโs prototype chain until proto is null.
๐ฅIn the loop, the code checks whether the prototype of the right operand right is equal to the current proto. If they are equal, it means that the left operand left is an instance of the right operand right and returns true.
๐ If no matching prototype is found at the end of the loop, that is, proto is null, it means that the left operand left is not an instance of the right operand right, and false is returned.
Please open Telegram to view this post
VIEW IN TELEGRAM
Please open Telegram to view this post
VIEW IN TELEGRAM