投稿時間:2023-08-21 17:19:17 RSSフィード2023-08-21 17:00 分まとめ(19件)

カテゴリー等 サイト名等 記事タイトル・トレンドワード等 リンクURL 頻出ワード・要約等/検索ボリューム 登録日
IT 気になる、記になる… 2023年上半期に最も売れたデスクトップPCはNECの「LAVIE A23」 − BCNランキングがTOP10を公開 https://taisy0.com/2023/08/21/175579.html laviea 2023-08-21 07:50:55
IT 気になる、記になる… Apple Japan、最新のサポート動画「Appleサポートアプリのご紹介」を公開 https://taisy0.com/2023/08/21/175577.html apple 2023-08-21 07:30:25
IT ITmedia 総合記事一覧 [ITmedia PC USER] AMD、同社製チップセット向け最新ドライバ「5.08.02.027」を公開 https://www.itmedia.co.jp/pcuser/articles/2308/21/news146.html itmediapcuseramd 2023-08-21 16:27:00
TECH Techable(テッカブル) 問題集作成サービス「SurpassOne」、法人向けサービス開始。 “自動生成AI”で教材の量産、人件費削減を実現 https://techable.jp/archives/217529 chatgpt 2023-08-21 07:00:22
AWS lambdaタグが付けられた新着投稿 - Qiita aws-lambda-power-tuningのS3payloadを利用する https://qiita.com/jyakuko/items/51469a24789498984cc4 awslambdapowertuning 2023-08-21 16:39:06
python Pythonタグが付けられた新着投稿 - Qiita 【2023年】プログラミング・Pythonの無料おすすめ学習教材13選 https://qiita.com/skillup_ai/items/75e892e166f41ca85bf8 編集部 2023-08-21 16:59:39
AWS AWSタグが付けられた新着投稿 - Qiita aws-lambda-power-tuningのS3payloadを利用する https://qiita.com/jyakuko/items/51469a24789498984cc4 awslambdapowertuning 2023-08-21 16:39:06
Azure Azureタグが付けられた新着投稿 - Qiita AZ-900に合格しました!! https://qiita.com/ayane-murakami/items/f1fc66c93eee5df56a31 無事 2023-08-21 16:42:05
海外TECH DEV Community Embarking on a React Adventure? First, Master These 5 JS concepts! 🚀 https://dev.to/ale3oula/embarking-on-a-react-adventure-first-master-these-5-js-concepts-211b Embarking on a React Adventure First Master These JS concepts So you decided to dive into the world of React a library used by millions of people and promises to make building user interfaces a breeze Before building your first to do list you might want to master some JavaScript concepts first Here are five fundamental JavaScript concepts you should master before venturing into React Unveiling the DOM Magic Understanding the Document Object Model DOM is key to comprehending React s significance The DOM represents the structure of a web page as a tree of objects allowing developers to interact with webpage elements using JavaScript Think of the DOM as a virtual representation of your webpage s content It takes all those HTML tags CSS styles and JavaScript interactions and turns them into a cohesive performance React operates in harmony with the DOM Updating a React component triggers a process that reconciles the virtual DOM a lightweight representation of the actual DOM with the real DOM This ensures that only the necessary parts of the page are updated making your applications efficient and responsive The Dance of the Component LifecycleReact components have a lifecycle consisting of different stages from initialization to rendering updating and unmounting Each stage presents opportunities to perform actions such as fetching data updating the state and cleaning up resources With the introduction of React hooks in versions and later lifecycle methods have been augmented by hooks like useState and useEffect These hooks simplify state management and side effect handling within components Casting ES CharmsBeneath every React component there is a foundation of modern JS concepts Some valuable reads will be Arrow functions Destructuring Classes and Modules Don t forget to master your basics about const and let which gives you better control of your variables State and props sorceryThe heartbeat of React components is definitely their state and props State You can manage your local mutable data within a component Changing the state triggers powerful re rendering spells that breathe life into your UI Props These flow from parent to child components allowing them to communicate harmoniously JavaScript Tooling Your Wizard s ToolkitModern web development relies on various tools and libraries Familiarize yourself with essential tools like Node js and npm yarn Package managers for installing and managing dependencies Webpack or Parcel Bundlers that optimize assets for production Babel A transpiler for writing modern JavaScript that is compatible with various browsers 2023-08-21 07:36:59
海外TECH DEV Community Complete Guide to JavaScript Promises, Async/await and Promise Methods https://dev.to/myogeshchavan97/complete-guide-to-javascript-promises-asyncawait-and-promise-methods-5aom Complete Guide to JavaScript Promises Async await and Promise MethodsIn this tutorial you will learn everything you need to know about using promises and async await in JavaScript So let s get started Want a video version of this tutorial You can check out my YouTube playlist Why Use Promises in JavaScript ES introduced promises as a native implementation Before ES we were using callbacks to handle asynchronous operations Let s understand what callbacks are and what problem related to callbacks is solved by promises Let s say we have a list of posts and their respective comments like this const posts post id post title First Post post id post title Second Post post id post title Third Post const comments post id comment Great post id comment Nice Post post id comment Awesome Post Now we will write a function to get a post by passing the post id If the post is found we will retrieve the comments related to that post const getPost id callback gt const post posts find post gt post post id id if post callback null post else callback No such post found undefined const getComments post id callback gt const result comments filter comment gt comment post id post id if result callback null result else callback No comments found undefined In the above getPost and getComments functions if there is an error we will pass it as the first argument to the callback function But if we get the result we will call the callback function and pass the result as the second argument to it If you are familiar with Node js then you will know that this is a very common pattern used in every Node js callback function Now let s use those functions getPost error post gt if error return console log error console log Post post getComments post post id error comments gt if error return console log error console log Comments comments After executing the above code you will see the following output Here s a CodePen Demo As you can see we have the getComments function nested inside the getPost callback Now imagine if we also wanted to find the likes of those comments That would also get nested inside getComments callback creating more nesting This will eventually make the code difficult to understand This nesting of the callbacks is known as callback hell You can see that the error handling condition also gets repeated in the code which creates duplicate code this is not good So to fix this problem and allow asynchronous operations promises were introduced What are Promises in JavaScript Promises are one of the most important parts of JavaScript but they can be confusing and difficult to understand Many new devs as well as experienced ones struggle to fully grasp them So what is a promise A promise represents an asynchronous operation whose result will come in the future Before ES there was no way to wait for something to perform some operation For example when we wanted to make an API call there was no way to wait until the results came back For that we used to use external libraries like JQuery or Ajax which had their own implementation of promises But there was no JavaScript implementation of promises But then promises were added in ES as a native implementation And now using promises in ES we can make an API call ourselves and wait until it s done to perform some operation How to Create a PromiseTo create a promise we need to use the Promise constructor function like this const promise new Promise function resolve reject The Promise constructor takes a function as an argument and that function internally receives resolve and reject as parameters The resolve and reject parameters are actually functions that we can call depending on the outcome of the asynchronous operation A Promise can go through three states PendingFulfilledRejectedWhen we create a promise it s in a pending state When we call the resolve function it goes in a fulfilled state and if we call reject it will go into the rejected state To simulate the long running or asynchronous operation we will use the setTimeout function const promise new Promise function resolve reject setTimeout function const sum resolve sum Here we ve created a promise which will resolve to the sum of and after a ms second timeout is over To get the result of the successful promise execution we need to register a callback handler using then like this const promise new Promise function resolve reject setTimeout function const sum resolve sum promise then function result console log result So whenever we call resolve the promise will return back the value passed to the resolve function which we can collect using the then handler If the operation is not successful then we call the reject function like this const promise new Promise function resolve reject setTimeout function const sum a if isNaN sum reject Error while calculating sum else resolve sum promise then function result console log result Here if the sum is not a number then we call the reject function with the error message Otherwise we call the resolve function If you execute the above code you will see the following output As you can see we re getting an uncaught error message along with the message we ve specified because calling the reject function throws an error But we have not added an error handler for catching that error To catch the error we need to register another callback using catch like this promise then function result console log result catch function error console log error You will see the following output As you can see we have added the catch handler so we re not getting any uncaught error we re just logging the error to the console This also avoids stopping your application abruptly So it s always recommended to add the catch handler to every promise so your application will not stop running because of the error When to Use resolve and rejectLet s take an example of an API call If you re making an API call and the API call is successful then you call the resolve function by passing the result of the API as an argument to it And if the API is unsuccessful then you call the reject function by passing any message as an argument to it So to indicate that the operation is successful we call the resolve function and for indicating an unsuccessful operation we call the reject function What is Promise Chaining and Why is it Useful Promise chaining is a technique used to manage asynchronous operations in a more organized and readable way In promise chaining we can attach multiple then handlers in which result of previous then handler is automatically passed to the next then handler Using promise chaining helps to avoid the problem of callback hell which we have seen previously Promise chaining also allows us to write asynchronous code in a more linear and sequential manner which is easier to read and understand Also when using promise chaining we can attach only one catch handler at the end of all the then handlers If any of the in between promises fail the last catch handler will be automatically executed So we don t need to add multiple catch handlers This eliminates multiple error check as we did in the callback hell example previously How Promise Chaining WorksWe can add multiple then handlers to a single promise like this promise then function result console log first then handler return result then function result console log second then handler console log result catch function error console log error When we have multiple then handlers added the return value of the previous then handler is automatically passed to the next then handler As you can see adding resolves a promise and we get that sum in the first then handler There we re printing a log statement and return that sum to the next then handler And inside the next then handler we re adding a log statement and then we re printing the result we got from the previous then handler This way of adding multiple then handlers is known as promise chaining How to Delay a Promise s Execution in JavaScriptMany times we don t want the promise to execute immediately Rather we want it to delay until after some operation is completed To achieve this we can wrap the promise in a function and return that promise from that function like this function createPromise return new Promise function resolve reject setTimeout function const sum if isNaN sum reject Error while calculating sum else resolve sum This way we can use the function parameters inside the promise making the function truly dynamic function createPromise a b return new Promise function resolve reject setTimeout function const sum a b if isNaN sum reject Error while calculating sum else resolve sum createPromise then function output console log output ORcreatePromise then function output console log output Note When we create a promise it will be either resolved or rejected but not both at the same time So we cannot add two resolve or reject function calls in the same promise Also we can pass only a single value to the resolve or reject function If you want to pass multiple values to a resolve function pass it as an object like this const promise new Promise function resolve reject setTimeout function const sum resolve a b sum promise then function result console log result catch function error console log error How to Use Arrow Functions in JavaScriptIn all the above code examples we ve used regular ES function syntax while creating promises But it s a common practice to use arrow function syntax instead of ES function syntax So let s first understand what is an arrow function and how to use it What are arrow functions Before ES there were two main ways of declaring functions Function Declaration Syntax function add a b return a b Function Expression Syntax const add function a b return a b The main visible difference between the regular function and arrow function is the syntax of writing the function Using arrow function syntax we can write the above adding function like this const add a b gt return a b You might not see much difference here apart from the arrow But if we have a single line of code in the function body we can simplify the above arrow function like this const add a b gt a b Here we are implicitly returning the result of a b so there is no need for a return keyword if there is a single statement So using the arrow functions will make your code much shorter If you want to learn other features of arrow functions you can check out this video Using an arrow function we can write the previous code as shown below const promise new Promise resolve reject gt setTimeout gt const sum a if isNaN sum reject Error while calculating sum else resolve sum promise then result gt console log result You can either use ES or ES function syntax depending on your preferences and needs How to Use Async Await in JavaScriptIn this section we ll explore everything you need to know about async await In this section we ll explore everything you need to know about async await Async await gives developers a better way to use promises To use async await you need to create a function and add the async keyword before the function name using ES function declaration syntax like this async function someFunction function body or using function expression syntax like this const someFunction async function function body or using an arrow function like this const someFunction async gt function body Always remember that when you add the async keyword to the function it always returns a promise Take a look at the below code const sayHello async function return Hello sayHello What do you think the output of the above code will be The output is a promise fulfilled with the string Hello So the below code const sayHello async function return Hello is the same as this const sayHello function return new Promise resolve reject gt resolve Hello which is the same as this const sayHello function return Promise resolve Hello Promise resolve Hello is just a shorter way of creating a promise which resolves to the string Hello So to get the actual string Hello we need to add the then handler like this sayHello then function result console log result Hello Promise resolve Hello is just a shorter way of creating a promise which resolves to string Hello So to get the actual string Hello we need to add then handler like this Now where do we use the await keyword It s used inside the function which is declared as async So the await keyword should only be used inside the async function You will get an error if you try to use it in non async functions Suppose we have a promise which returns the product of two numbers like this function getProduct a b return new Promise function resolve reject setTimeout function resolve a b and we re using it like this getProduct then function result getProduct result then function finalResult console log final result finalResult catch function error console log error catch function error console log error In the above code we re first getting the product of and Then we re using that result to multiply it by again and then finally printing the product If you execute the above code you will see the final result as which is and The above code of   then and catch looks pretty complicated and difficult to understand at one glance So using async await we can simplify the above code to this const printResult async gt try const result await getProduct line const finalResult await getProduct result line console log final result finalResult line catch error console log error printResult This looks much cleaner and easy to understand Here to use the await keyword we re declaring a function with the async keyword Then to get the result of each promise we re adding the await keyword in front of it Also note that we ve added try catch inside the function You always need to add a try block around the code which uses await so the catch block will be executed if the promise gets rejected There is a very important thing you need to remember The above async await code will work exactly the same as when we use then so the next await line line will not be executed until the previous await call line is successful Therefore as the getProduct function is taking second to execute because of the setTimeout call line will have to wait for second before executing the getProduct function again But there is one exception to this behavior which you can check out in this article Also if there is an error while executing line because of some error that occurred in the getProduct function the next code after line will not be executed Instead the catch block will be executed Now if you compare the code of promise chaining and async await you will see the difference code using async awaitconst printResult async gt try const product await getProduct line const finalResult await getProduct product line console log final result finalResult line catch error console log error printResult code using then and catchgetProduct then function result getProduct result then function finalResult console log final result finalResult catch function error console log error catch function error console log error As the nesting gets deeper the code using promise chaining gets more complicated so async await just provides a way to write the same code but with better clarity Using async await also reduces the need of adding multiple catch handlers to handle the errors We can certainly avoid the nesting in the above promise chaining by writing the previous code like this getProduct then function result return getProduct result then function finalResult console log final result finalResult catch function error console log error Here from the first then handler we re returning the result of getProduct result Whatever returned from the previous then handler will be passed to the next then handler As the getProduct function returns a promise so we can attach then again to it and avoid the need for a nested catch handler But still async await syntax looks cleaner and easier to understand than the promise chaining syntax Promise MethodsIn this section we ll explore the various methods provided by the Promise API All these methods are useful when you want to execute multiple asynchronous tasks at the same time when those tasks are not dependent on each other which saves a lot of time Because if you execute each task one after the other then you have to wait for the previous task to finish before you can start with the next task And if the tasks are not related to each other there is no point in waiting for the previous task to finish before executing the next task The Promise all methodThis method is used to execute multiple asynchronous tasks simultaneously without having to wait for another task to finish Suppose we have three promises and all are resolved successfully const promise new Promise resolve reject gt resolve promise success const promise new Promise resolve reject gt resolve promise success const promise new Promise resolve reject gt resolve promise success Now let s use the Promise all method Promise all needs an array of promises as its argument Promise all promise promise promise then result gt console log resolved result resolved promise success promise success promise success catch error gt console log rejected error As all the promises are resolved result will be an array containing the results of the resolved promises Now what if any of the promises get rejected const promise new Promise resolve reject gt resolve promise success const promise new Promise resolve reject gt reject promise failure const promise new Promise resolve reject gt resolve promise success Promise all promise promise promise then result gt console log resolved result catch error gt console log rejected error rejected promise failure In the above code promise is rejected so the catch handler will be executed and in the case of Promise all If one of the promises is rejected the error will contain the error message of the failed promise as in our case above If multiple promises are rejected the error will be the error message of the first failed promise Note Even though the intermediate promise gets rejected all next promises will not be stopped from executing They will all be executed but only the first rejected promise value will be available in the error parameter of the catch block The Promise race methodConsider again the three resolved promises const promise new Promise resolve reject gt resolve promise success const promise new Promise resolve reject gt resolve promise success const promise new Promise resolve reject gt resolve promise success Promise race promise promise promise then result gt console log resolved result resolved promise success catch error gt console log rejected error As you can see here as soon as the first promise gets resolved the Promise race method will return the result of that resolved promise Now take a look at the below code const promise new Promise resolve reject gt reject promise failure const promise new Promise resolve reject gt resolve promise success const promise new Promise resolve reject gt resolve promise success Promise race promise promise promise then result gt console log resolved result catch error gt console log rejected error rejected promise failure As you can see here the first promise itself is rejected so the catch handler will be executed So when we use the Promise race method it will wait until the first promise gets resolved or rejected and then If the first promise in the promise chain gets resolved the then handler will be executed and the result will be the result of the first resolved promise If the first promise in the promise chain gets rejected the catch handler will be executed and the result will be the result of the first failed promise If multiple promises are rejected the catch handler will be executed and the result will be the result of the first failed promise The Promise allSettled methodThis method is useful when you want to know the result of each task even though they are rejected Because in Promise all and Promise race we get only the result of the first rejected promise and there is no way to get the result of other successful or failed promises So using Promise allSettled we can get the result of all the promises even if they failed Take a look at the below code const promise new Promise resolve reject gt resolve promise success const promise new Promise resolve reject gt resolve promise success const promise new Promise resolve reject gt resolve promise success Promise allSettled promise promise promise then result gt console log resolved result output from then resolved status fulfilled value promise success status fulfilled value promise success status fulfilled value promise success As you can see the Promise allSettled method waits until all the promises are resolved or rejected and the result will contain the result of each promise const promise new Promise resolve reject gt reject promise failure const promise new Promise resolve reject gt resolve promise success const promise new Promise resolve reject gt resolve promise success Promise allSettled promise promise promise then result gt console log resolved result output from then resolved status rejected reason promise failure status fulfilled value promise success status fulfilled value promise success In the above case even though the first promise is rejected we get the result of all the promises inside the then handler const promise new Promise resolve reject gt reject promise failure const promise new Promise resolve reject gt reject promise failure const promise new Promise resolve reject gt reject promise failure Promise allSettled promise promise promise then result gt console log resolved result output from then resolved status rejected reason promise failure status rejected reason promise failure status rejected reason promise failure Here even though all the promises are rejected still the then handler will be executed and we get the result of each promise Want to learn how to use these promise methods in an actual React application Check out my previous article Thanks for Reading That s it for this tutorial I hope you learned a lot from it Want a video version of this tutorial check out my YouTube playlist If you want to master JavaScript ES React and Node js with easy to understand content do check out my YouTube channel and don t forget to subscribe Want to stay up to date with regular content regarding JavaScript React Node js Follow me on LinkedIn 2023-08-21 07:22:45
医療系 医療介護 CBnews 在宅医療と介護の連携推進、市町村の対応策支援も-熊本県が高齢者福祉計画などの自己評価結果を公表 https://www.cbnews.jp/news/entry/20230821163913 介護保険 2023-08-21 16:50:00
ニュース BBC News - Home Nurse Lucy Letby to be sentenced for murdering seven babies https://www.bbc.co.uk/news/uk-england-merseyside-66545358?at_medium=RSS&at_campaign=KARANGA criminals 2023-08-21 07:12:42
ニュース BBC News - Home John Stones: Manchester City defender ruled out until after international break with injury https://www.bbc.co.uk/sport/football/66567715?at_medium=RSS&at_campaign=KARANGA John Stones Manchester City defender ruled out until after international break with injuryManchester City and England defender John Stones is ruled out of action until after the international break with a muscle injury 2023-08-21 07:07:29
ニュース BBC News - Home What next for England and Wiegman? https://www.bbc.co.uk/sport/football/66564402?at_medium=RSS&at_campaign=KARANGA world 2023-08-21 07:01:08
IT 週刊アスキー 上品な“だし”とサクサクのごぼう天をカップ麺で味わえる! サンポー食品「三宝だし本家 博多ごぼう天うどん」 https://weekly.ascii.jp/elem/000/004/150/4150795/ 食品 2023-08-21 16:40:00
IT 週刊アスキー X(Twitter)、ブロック機能を廃止へ「もっといいものができる」と https://weekly.ascii.jp/elem/000/004/150/4150745/ xtwitter 2023-08-21 16:30:00
IT 週刊アスキー ホテルニューオータニ博多、ハロウィンの世界観を表現した「ハロウィンパンビュッフェ」 https://weekly.ascii.jp/elem/000/004/150/4150750/ 表現 2023-08-21 16:10:00
IT 週刊アスキー ピザーラ「ハワイアンデライト」が最大550円引き!今だけ https://weekly.ascii.jp/elem/000/004/150/4150771/ pizzala 2023-08-21 16:30:00
IT 週刊アスキー ここまで素朴になった ローソンストア「漬物だけ弁当」216円 https://weekly.ascii.jp/elem/000/004/150/4150773/ 素朴 2023-08-21 16:15:00

コメント

このブログの人気の投稿

投稿時間:2021-06-17 22:08:45 RSSフィード2021-06-17 22:00 分まとめ(2089件)

投稿時間:2021-06-20 02:06:12 RSSフィード2021-06-20 02:00 分まとめ(3871件)

投稿時間:2021-06-17 05:05:34 RSSフィード2021-06-17 05:00 分まとめ(1274件)