Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                
              
            
!

CSS

              
                
              
            
!

JS

              
                console.clear()

///////////////
/// Promise ///
///////////////
function getSomeApiResult(boo, errorHintStr) {
	return new Promise(function(resolve, reject){
    setTimeout(function(){
      if (boo) {
        resolve('promise success!');
      } else {
        reject(errorHintStr)
      }
    }, 500)
	})
}

// ## 第一次執行
getSomeApiResult(true, 'promise1 error!')
  .then((result) =>{ // result 值為 Promise 建構式的 resolve 傳入的值
    console.log(result)
// ## 第二次執行
    return getSomeApiResult(false, 'promise2 error!') // return後才可使用鏈式繼續串接then()
  })
  .then((result) =>{
    console.log(result)
// ## 第三次執行
    return getSomeApiResult(false, 'promise3 error!') // 由於第二次 Promise 失敗後直接跳入 catch 所以這個 Promise 不會執行
  })
  .then((result) =>{
    console.log(result)	
  })
  // 第二次 getSomeApiResult 故意帶入 false 參數, 模擬 API 失敗時的錯誤處理, getSomeApiResult 內模擬的錯誤判斷成立, 因此跳入此 catch block 做 error handling
  .catch((error) => { // error 的值為 Promise 建構式的 reject 傳入的值
    console.log(error)
  })


////////////////////
/// async  await ///
////////////////////
function getSomeApiResult_2(boo, errorHintStr) {
  return new Promise ((resolve, reject) => {
    setTimeout(() => {
      if (boo) {
        resolve('Async success!')
      } else {
        reject(errorHintStr)
      }
    }, 500)
   })
}

// await 後面必須接 Promise 函式, 且執行 await 的 function 前面必須加上 async
// 否則報錯 Uncaught SyntaxError: await is only valid in async function
async function doApiCall() {
  try {
    // 取得 promise 函式的 resolve 值, 回傳給 result
    let result = await getSomeApiResult_2(true, 'Async1 error!')
    console.log(result)
    
    let result2 = await getSomeApiResult_2(false, 'Async2 error!')
    console.log(result2)
    
    let result3 = await getSomeApiResult_2(true, 'Async3 error!')
    console.log(result3)
    
  } catch (error) { // 錯誤時, 跑 catch block 內的程式碼, 做 error handling
    console.log(error)
  }
}


doApiCall()
              
            
!
999px

Console